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\),现在想把 ...
随机推荐
- MYSQL——root密码更换
方法1: 用SET PASSWORD命令mysql -u rootmysql> SET PASSWORD FOR 'root'@'localhost' = PASSWORD('newpass') ...
- poj 3686 Priest John's Busiest Day
http://poj.org/problem?id=3683 2-sat 问题判定,输出一组可行解 http://www.cnblogs.com/TheRoadToTheGold/p/8436948. ...
- Django Book 学习笔记(上)
拜读了网上的Django Book,现在来总结一下吧...... 一.Django的配置 非常的蛋疼,由于Django的块组之间耦合度低,这既是它的优点,也是它的缺点.我在Ubuntu所配置的Djan ...
- AC自动机(Keywords Search)
题目链接:https://cn.vjudge.net/contest/280743#problem/A 题目大意:首先给你T组测试样例,然后给你n个字符串,最后再给你一个模式串,然后问你这一些字符串中 ...
- 【Udacity并行计算课程笔记】- lesson 1 The GPU Programming Model
一.传统的提高计算速度的方法 faster clocks (设置更快的时钟) more work over per clock cycle(每个时钟周期做更多的工作) more processors( ...
- Maven私服安装及配置——(十二)
0.私服实际是B/S架构的,需要通过浏览器访问.访问地址在 nexus-2.12.0-01\conf\nexus.properties中查看.
- Servlet笔记7--HttpServletRequest介绍
通过HttpServletRequest获取表单提交的数据: 前端页面: <html> <head> <title>register</title> & ...
- oracel回收站清理
从powerdesigner中往oracle中导入表,出现了很多类似“BIN$Z35FPY7eFZDgUKjAC94NkA==$0 ”这样的表名, 原因是删除表的时候没有彻底的删除表,而是把表放入回收 ...
- C++单链表反转
单链表反转笔记: #include<iostream> #include<string.h> using namespace std; struct ListNode { in ...
- Linux USB Host-Controller的初始化代码框架分析【转】
转自:http://blog.csdn.net/zkami/article/details/2496770 usb_hcd_omap_probe (const struct hc_driver *dr ...