Atcoder Grand Contest 010 C - Cleaning 树贪心(伪)
C - Cleaning
题目连接:
http://agc010.contest.atcoder.jp/tasks/agc010_c
Description
There is a tree with N vertices, numbered 1 through N. The i-th of the N−1 edges connects vertices ai and bi.
Currently, there are Ai stones placed on vertex i. Determine whether it is possible to remove all the stones from the vertices by repeatedly performing the following operation:
Select a pair of different leaves. Then, remove exactly one stone from every vertex on the path between those two vertices. Here, a leaf is a vertex of the tree whose degree is 1, and the selected leaves themselves are also considered as vertices on the path connecting them.
Note that the operation cannot be performed if there is a vertex with no stone on the path.
Input
The input is given from Standard Input in the following format:
N
A1 A2 … AN
a1 b1
:
aN−1 bN−1
2≦N≦105
1≦ai,bi≦N
0≦Ai≦109
The given graph is a tree.
Output
If it is possible to remove all the stones from the vertices, print YES. Otherwise, print NO.
Sample Input
5
1 2 1 1 2
2 4
5 2
3 2
1 3
Sample Output
YES
Hint
题意
给你一棵树,你每次可以选择两个叶子节点,使得这条路径上的所有点的点权减1,问你能否全部变成0.
题解:
考虑只有一层的时候,即一个点和一堆叶子,需要满足哪些条件,才能使得所有叶子节点的权值为0呢:
1.叶子权值和一定要大于等于父亲节点的权值,因为这样父亲节点才能满足下面的叶子节点的消耗。
2.叶子权值和的两倍要小于等于父亲节点的权值,因为叶子节点的权值每次是-2的,而父亲节点是-1.
3.叶子权值的最大值应该小于等于父亲节点。
根据这三个规则,一直递归的使得底层的点处理完之后,等价的看为叶子节点,然后不停跑就好了,有点树形dp的感觉……
代码
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+6;
int n,a[maxn];
vector<int> E[maxn];
void dfs(int x,int p){
if(E[x].size() == 1)return;
long long sum = 0;
long long mx = 0;
for(int i=0;i<E[x].size();i++){
int v = E[x][i];
if(v==p)continue;
dfs(v, x);
sum += a[v];
mx = max(1ll*a[v], mx);
}
if(a[x]>sum||sum>2*a[x]){
cout<<"NO"<<endl;
exit(0);
}
int k=sum-a[x];
if(k>sum-mx){
cout<<"NO"<<endl;
exit(0);
}
a[x]-=k;
}
int main()
{
scanf("%d", &n);
for(int i=0;i<n;i++)
scanf("%d", &a[i]);
for(int i=1;i<n;i++){
int x,y;
scanf("%d%d", &x, &y);
x--,y--;
E[x].push_back(y);
E[y].push_back(x);
}
if(n==2){
if(a[0]==a[1])puts("YES");
else puts("NO");
return 0;
}
int v=0;
while(E[v].size() == 1) v++;
dfs(v, -1);
if(a[v]==0)cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
Atcoder Grand Contest 010 C - Cleaning 树贪心(伪)的更多相关文章
- AtCoder Grand Contest 010
AtCoder Grand Contest 010 A - Addition 翻译 黑板上写了\(n\)个正整数,每次会擦去两个奇偶性相同的数,然后把他们的和写会到黑板上,问最终能否只剩下一个数. 题 ...
- AtCoder Grand Contest 010 C:Cleaning
题目传送门:https://agc010.contest.atcoder.jp/tasks/agc010_c 题目翻译 给你一棵树,每个点有个权值,每次操作可以选择两个度数为\(1\)的结点,然后让这 ...
- AtCoder Grand Contest 010 F - Tree Game
题目传送门:https://agc010.contest.atcoder.jp/tasks/agc010_f 题目大意: 给定一棵树,每个节点上有\(a_i\)个石子,某个节点上有一个棋子,两人轮流操 ...
- Atcoder Grand Contest 010 B - Boxes 差分
B - Boxes 题目连接: http://agc010.contest.atcoder.jp/tasks/agc010_b Description There are N boxes arrang ...
- AtCoder Grand Contest 010题解
传送门 \(A\) 判一下奇数的个数就行了 const int N=1e5+5; int a[N],n,res; int main(){ scanf("%d",&n); f ...
- AtCoder Grand Contest 010 D - Decrementing
题目描述 有n个整数,其中第i个数为Ai.这些数字的gcd为1.两人轮流操作,每次操作把一个大于1的数减1,并把所有数除以所有数的最大公约数,最后无法操作者输,求是否先手必胜. 如果当前的sum为偶数 ...
- AtCoder Grand Contest 011
AtCoder Grand Contest 011 upd:这篇咕了好久,前面几题是三周以前写的... AtCoder Grand Contest 011 A - Airport Bus 翻译 有\( ...
- AtCoder Grand Contest 009
AtCoder Grand Contest 009 A - Multiple Array 翻译 见洛谷 题解 从后往前考虑. #include<iostream> #include< ...
- AtCoder Grand Contest 008
AtCoder Grand Contest 008 A - Simple Calculator 翻译 有一个计算器,上面有一个显示按钮和两个其他的按钮.初始时,计算器上显示的数字是\(x\),现在想把 ...
随机推荐
- 1、Saltstack简介及安装配置
1.Saltstack简介 Saltstack是基于Python开发的一套C/S架构,具备Puppet.Ansible功能于一身的配置管理工具,功能十分强大,各模块融合度及复用性极高:使用号称世界上最 ...
- git与代码托管工具
1.git初识 git是一个版本管理工具,用来管理项目的不同的版本,记录下不同的提交记录,git还可以构建不同的分支,用来给不同的人来推送使用. 推荐的git教程:https://www.cnblog ...
- Linux TTY驱动--Serial Core层【转】
转自:http://blog.csdn.net/sharecode/article/details/9197567 版权声明:本文为博主原创文章,未经博主允许不得转载. 接上一节: Linux TTY ...
- 【坐在马桶上看算法】算法4:队列——解密QQ号
新学期开始了,小哈是小哼的新同桌(小哈是个小美女哦~),小哼向小哈询问QQ号,小哈当然不会直接告诉小哼啦,原因嘛你懂的.所以小哈给了小哼一串加密过的数字,同时小哈也告诉了小哼解密规则. ...
- H5页面关于android软键盘弹出顶起底部元素的解决方案
应用场景:用div在移动端页面设置一个底部工具栏,css的代码大概如下: .tool{ width: 100%; height: 60px; position: fixed; left: 0px; b ...
- RHEL7 -- 使用Chrony设置时间与时钟服务器同步
Chrony是一个开源的自由软件,它能保持系统时钟与时钟服务器(NTP)同步,让时间保持精确. 它由两个程序组成:chronyd和chronyc. chronyd是一个后台运行的守护进程,用于调整内核 ...
- [golang note] 环境搭建
LiteIDE(windows) • golang安装 ▶ 下载对应操作系统的版本并安装,下载地址:http://www.golangtc.com/download,譬如这里下载的是go1.6.win ...
- Java实现继承过程概述
super(); 在调用子类的构造器的时候,如果没有显示的写出 super(); ,那么,编译器会在佛那个加上 super(); 无参构造器 如果想调用父类的有参构造器,那么,必须显示的调用,编译器不 ...
- JAVA学习(二) String使用equals方法和==分别比较的是什么?(转)
String使用的equals方法和==的区别 equals方法和==的区别 首先大家知道,String既可以作为一个对象来使用,又可以作为一个基本类型来使用.这里指的作为一个基本类型来使用只是指 ...
- kafka查看消费数据
一.如何查看 在老版本中,使用kafka-run-class.sh 脚本进行查看.但是对于最新版本,kafka-run-class.sh 已经不能使用,必须使用另外一个脚本才行,它就是kafka-co ...