当前位置:网站首页>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;
}
边栏推荐
- list和dict的应用
- Seeking magic square of order n with C language
- 多源BFS问题 模板(附题)
- Pytoch official fast r-cnn source code analysis (I) -- feature extraction
- Codeforces 1629 F1. Game on sum (easy version) - DP, game, thinking
- The problem of Joseph in Informatics
- Application of binary search -- finding the square root sqrt of a number
- leetcode 47. Permutations II 全排列 II(中等)
- Innovation training (XII) project summary
- 1004: character triangle
猜你喜欢

Actual combat | realizing monocular camera ranging by skillfully using pose solution

Title: Yanghui triangle

Cocoapods的相关知识点

There was an error installing mysql. Follow the link below to CMD

Implementing pytorch style deep learning framework similartorch with numpy

Application of binary search -- finding the square root sqrt of a number
![2061: [example 1.2] trapezoidal area](/img/83/79b73ca10615c852768aba8d2a5049.jpg)
2061: [example 1.2] trapezoidal area

上海解封背后,这群开发者“云聚会”造了个AI抗疫机器人

微信web开发者工具使用教程,web开发问题

高通平台开发系列讲解(协议篇)QMI简单介绍及使用方法
随机推荐
单向环形链表实现约瑟夫环
C language implementation of string and memory library functions
C language array and pointer
Codeforces 1629 C. Mexico array - simple greed
VGA display color bar and picture (FPGA)
Rk3399 platform development series explanation (kernel debugging chapter) 2.50 use of systrace
The problem of Joseph in Informatics
Semantic segmentation with pytorch
Successful job hopping Ali, advanced learning
How to balance multiple losses in deep learning?
Record some settings for visual studio 2019
Informatics Olympiad all in one 2059: [example 3.11] buy a pen
view的子视图的递归
Codeforces 1629 F2. Game on sum (hard version) - Yang Hui's triangle, violence, finding rules
字节序数据读写
torch_ About the geometric Mini batch
static 和 extern 关键字详解
[cloud native | kubernetes] actual combat of ingress case
[cloud native | kubernetes] in depth understanding of deployment (VIII)
Chaotic engineering practice of distributed kV storage in station B