当前位置:网站首页>Codeforces 1637 F. Towers - thinking, DFS
Codeforces 1637 F. Towers - thinking, DFS
2022-06-12 13:33:00 【Tianyi City*】
The question :
Give you a tree , Each point has a value a[i]. You have to put value on some points b[i], So that for any point x, There are two points u,v:x stay u,v And min(b[u],b[v])>=a[x].
Ask you the least b What is the sum .
Answer key :
I put ne It has been written. x, For half an hour bug.
We just need to put a The largest point is the root rt, And then for any one x(x!=rt), Only one of its subtrees is needed b>=a[x] that will do .
Because in the end we need the value of two points >=a[rt], So for any one x(x!=rt) Come on , There must be at least one point not in its subtree .
We record mx[x] Represents the current subtree b The maximum of ,smx Means different from mx The maximum value in other subtrees of .
If the current is the root , Then we need two points to support him , Otherwise, only one point is needed .
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N=2e5+5;
vector<int>vec[N];
int a[N],mx[N],smx[N],rt;
ll ans;
void dfs(int x,int fa){
if(x-rt&&vec[x].size()==1)mx[x]=a[x],ans+=a[x];
for(int ne:vec[x]){
if(ne==fa)continue;
dfs(ne,x);
if(mx[ne]>mx[x])smx[x]=mx[x],mx[x]=mx[ne];
else if(mx[ne]>smx[x])smx[x]=mx[ne];
}
if(x-rt){
if(mx[x]<a[x])
ans+=a[x]-mx[x],mx[x]=a[x];
}
else{
if(!smx[x])ans+=a[x]+a[x]-mx[x];
else ans+=a[x]*2-mx[x]-smx[x];
}
}
int main()
{
int n,x,y;
scanf("%d",&n);
for(int i=1;i<=n;i++)scanf("%d",&a[i]),rt=a[rt]>=a[i]?rt:i;
for(int i=1;i<n;i++)
scanf("%d%d",&x,&y),vec[x].push_back(y),vec[y].push_back(x);
dfs(rt,0);
printf("%lld\n",ans);
return 0;
}
边栏推荐
- 2065: [example 2.2] sum of integers
- 【云原生 | Kubernetes篇】Kubernetes 网络策略(NetworkPolicy)
- jsp跳转问题,不能显示数据库数据,并且不能跳转
- AVFoundation
- There was an error installing mysql. Follow the link below to CMD
- 章鱼网络进展月报 | 2022.5.1-5.31
- 2064: [example 2.1] exchange value
- Script import CDN link prompt net:: err_ FILE_ NOT_ Found problem
- JSP jump problem, unable to display database data, and unable to jump
- 1005: estimation of the earth's population carrying capacity
猜你喜欢

创新实训(十一)开发过程中的一些bug汇总

VGA display color bar and picture (FPGA)

Innovation training (XI) summary of some bugs in the development process
颜色编码格式介绍

【刷题篇】抽牌获胜的概率

Script引入CDN链接提示net::ERR_FILE_NOT_FOUND问题

Title: Yanghui triangle

Pytorch framework

Qualcomm platform development series (Protocol) QMI brief introduction and usage

Wechat web developer tools tutorial, web development issues
随机推荐
leetcode 47. Permutations II full permutations II (medium)
Seekg, tellg related file operations
Redis消息队列重复消费问题
Time processing in C language (conversion between string and timestamp)
Experience and learning path of introductory deep learning and machine learning
单向环形链表实现约瑟夫环
Code debugging - print log output to file
import torch_ Geometric loads some common datasets
torch_geometric message passing network
Redis message queue repeated consumption
Web3.0,「激发创造」的时代
Codeforces 1629 F1. Game on sum (easy version) - DP, game, thinking
1003: align output
imagemagick:a gentle introduction to magick++
D1 Nezha Development Board understands the basic startup and loading process
Software construction 03 regular expression
Codeforces 1629 F2. Game on sum (hard version) - Yang Hui's triangle, violence, finding rules
IC chip scheme fs4062b for lithium battery charging with 5V boost to 12.6V
达梦数据库DM8 windows环境安装
Innovation training (x) advanced interface beautification