当前位置:网站首页>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;
}
边栏推荐
猜你喜欢

import torch_ Data view of geometric

torch_geometric message passing network

Summary of question brushing in leetcode sliding window

高通平台开发系列讲解(协议篇)QMI简单介绍及使用方法

leetcode 47. Permutations II 全排列 II(中等)
颜色编码格式介绍

"New continent" of mobile application going to sea

imagemagick:a gentle introduction to magick++

Pytorch to onnx, onnxruntime reasoning in mmclas

Qualcomm platform development series (Protocol) QMI brief introduction and usage
随机推荐
GPUImage链式纹理的简单实现
创新实训(十二)项目总结
Informatics Olympiad all in one 2059: [example 3.11] buy a pen
import torch_geometric 的Data 查看
2063: [example 1.4] cattle eat grass
Codeforces 1629 C. Mexico array - simple greed
import torch_ Geometric first graph network example
Hudi key generation
Tensorrt, onnx to tensorrt in mmclas
Application of short circuit expression (||) in C language
[brush title] probability of winning a draw
RK3399平台开发系列讲解(内核调试篇)2.50、systrace的使用
torch_ About the geometric Mini batch
字节序数据读写
It is enough to read this article. Web Chinese development
简历 NFT 平台 TrustRecruit 加入章鱼网络成为候选应用链
Scyther工具形式化分析Woo-Lam协议
播放器屏幕方向方案
jsp跳转问题,不能显示数据库数据,并且不能跳转
Title: Yanghui triangle