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. 【DS】排序算法之选择排序(Selection Sort)

    一.算法思想 选择排序是一种简单直观的排序算法.它的工作原理如下: 1)将序列分成两部分,前半部分是已经排序的序列,后半部分是未排序的序列: 2)在未排序序列中找到最小(大)元素,放到已排序序列的末尾 ...

  2. JMS学习(三)ActiveMQ Message Persistence

    1,JMS规范支持两种类型的消息传递:persistent and non-persistent.ActiveMQ在支持这两种类型的传递方式时,还支持消息的恢复.中间状态的消息(message are ...

  3. html5 canvas 径向渐变

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  4. CSS-3 文字阴影—text-shadow 的使用

    text-shadow还没有出现的时候,大家在网页中的阴影就是用ps一张图片作为背景.那么现在有了CSS3的这个属性,日后我们的工作会更简洁些. text-shadow之前出现过,不过不久就被Pass ...

  5. [python]文件操作read&readline&readlines

    (1)read是将整个文件读入内存,将整个文件的内容当作一个字符串 (2)readline是一行一行的读如内存,每一次读的一行为一个字符串 (3)readlines是一次将整个文件读入内存,但是将整个 ...

  6. JavaScript Cookies取值

    http://www.w3school.com.cn/js/js_cookies.asp

  7. shell邮件发送功能实现

    本文中以163邮箱为例,测试shell邮件发送功能.常见的工具有:mailx.sendmail.mutt等. 1.设置邮件客户端 (1)启用pop3.smtp服务,以支持第三方客户端支持 (2)设置授 ...

  8. 关于caffe的安装问题

    在caffe的安装过程中,出现 /usr/bin/ld: cannot find -lcblas /usr/bin/ld: cannot find -latlas的问题 这时解决方案为http://s ...

  9. Linux中断(interrupt)子系统之三:中断流控处理层【转】

    转自:http://blog.csdn.net/droidphone/article/details/7489756 1.  中断流控层简介 早期的内核版本中,几乎所有的中断都是由__do_IRQ函数 ...

  10. linux下brctl配置网桥

    原文:http://zhumeng8337797.blog.163.com/blog/static/1007689142011643834429/ 先装好网卡,连上网线,这是废话,不用说了. 然后开始 ...