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 树贪心(伪)的更多相关文章

  1. AtCoder Grand Contest 010

    AtCoder Grand Contest 010 A - Addition 翻译 黑板上写了\(n\)个正整数,每次会擦去两个奇偶性相同的数,然后把他们的和写会到黑板上,问最终能否只剩下一个数. 题 ...

  2. AtCoder Grand Contest 010 C:Cleaning

    题目传送门:https://agc010.contest.atcoder.jp/tasks/agc010_c 题目翻译 给你一棵树,每个点有个权值,每次操作可以选择两个度数为\(1\)的结点,然后让这 ...

  3. AtCoder Grand Contest 010 F - Tree Game

    题目传送门:https://agc010.contest.atcoder.jp/tasks/agc010_f 题目大意: 给定一棵树,每个节点上有\(a_i\)个石子,某个节点上有一个棋子,两人轮流操 ...

  4. Atcoder Grand Contest 010 B - Boxes 差分

    B - Boxes 题目连接: http://agc010.contest.atcoder.jp/tasks/agc010_b Description There are N boxes arrang ...

  5. AtCoder Grand Contest 010题解

    传送门 \(A\) 判一下奇数的个数就行了 const int N=1e5+5; int a[N],n,res; int main(){ scanf("%d",&n); f ...

  6. AtCoder Grand Contest 010 D - Decrementing

    题目描述 有n个整数,其中第i个数为Ai.这些数字的gcd为1.两人轮流操作,每次操作把一个大于1的数减1,并把所有数除以所有数的最大公约数,最后无法操作者输,求是否先手必胜. 如果当前的sum为偶数 ...

  7. AtCoder Grand Contest 011

    AtCoder Grand Contest 011 upd:这篇咕了好久,前面几题是三周以前写的... AtCoder Grand Contest 011 A - Airport Bus 翻译 有\( ...

  8. AtCoder Grand Contest 009

    AtCoder Grand Contest 009 A - Multiple Array 翻译 见洛谷 题解 从后往前考虑. #include<iostream> #include< ...

  9. AtCoder Grand Contest 008

    AtCoder Grand Contest 008 A - Simple Calculator 翻译 有一个计算器,上面有一个显示按钮和两个其他的按钮.初始时,计算器上显示的数字是\(x\),现在想把 ...

随机推荐

  1. bzoj千题计划267:bzoj3129: [Sdoi2013]方程

    http://www.lydsy.com/JudgeOnline/problem.php?id=3129 如果没有Ai的限制,就是隔板法,C(m-1,n-1) >=Ai 的限制:m减去Ai &l ...

  2. ASP.NET MVC学习(四)之视图View

    1.视图 2.强类型视图 3.@RenderSection("HeaderSection", false)     @RenderBody() 4.子行为 5.ASP.NET MV ...

  3. 程序员与HR博弈之:有城府的表达你的兴趣爱好

    “面试”这个过程说简单其实也能很简单.譬如急需招某种技能的单位会因为你拥有某方面的经验或特长立马录取你,哪怕你其他方面表现的很“烂”. 从广义上来讲,很多公司尤其是大中型公司的招聘,并不是因为急缺某岗 ...

  4. VMware Linux 下 Nginx 安装配置 (一)

    资源准备 1. pcre-8.34.tar.gz: ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/ 2. zlib-1.2.8.tar.g ...

  5. 第5月第13天 node cnpm安装 babel

    1. https://nodejs.org/en/download/ http://www.runoob.com/react/react-install.html 2. npm install --s ...

  6. 在Windows 2008上安装Windows Mobile设备中心

    我在windows2008系统上安装Microsoft Windows Mobile Device Center v6.1时,老是弹出对话框提示缺少一个Windows Mobile设备中心所需要的Wi ...

  7. (P2022 有趣的数)||(zoj Little Sub and Mr.Potato's Math Problem)(思维)

    题目链接:https://www.luogu.org/problemnew/show/P2022 题目大意:中文题目 具体思路: 第一步:我们可以先计算出当前的数前面按照字典序的话,前面有多少数(包括 ...

  8. Maven从私服上下载所需jar包——(十四)

    1.修改settings.xml 将下面代码添加到settings.xml中 <profile> <!--profile的id--> <id>dev</id& ...

  9. Kth Smallest Number in Sorted Matrix

    Find the kth smallest number in at row and column sorted matrix. Example Given k = 4 and a matrix: [ ...

  10. HTML播放FLASH(SWF)神器-SWFObject

    环境 必须有 swfobject.js和 expressInstall.swf js:  http://pan.baidu.com/share/link?shareid=2536087943& ...