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\),现在想把 ...
随机推荐
- 51nod 1258 序列求和 V4
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1258 1258 序列求和 V4 基准时间限制:8 秒 空间限制:131 ...
- vue注册全局属性
例:统一引用getSpiderToken方法 main.js中相关代码 import { getSpiderToken } from '../static/js/storage' Vue.protot ...
- 浅谈 js 下 with 对性能的影响
这几天多次看到有博主们在写 with 的文章,这货确实非常方便,但是却是个性能杀手,所以一直都是上不得台面的.那么他究竟会让效率低下到什么程度呢?先来看下 with 是如何的便捷吧.. // 正常调用 ...
- html5 canvas 弧形描边渐变
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- javascript完美拖拽的实现
直接上代码: HTML代码: <!DOCTYPE HTML> <html lang="en-US"> <head> <meta chars ...
- HDU 4502 吉哥系列故事——临时工计划(一维动态规划)
题意:吉哥的假期是1到n天,然后有m个工作可以让吉哥选择做,每个工作都有一个开始 t_s 和结束的时间 t_e ,都用天来表示,然后每个工作必须从第一天做到最后一天, 从头到尾做完之后就可以得到 ...
- [译]How To Use the Linux Auditing System on CentOS 7
本文是How To Use the Linux Auditing System on CentOS 7的中文版,翻译不到之处,还请指出和多多包涵.本文并不会完全遵从原文的一些格式,而是加入自己学习的理 ...
- Django之模板语法
Django框架之第三篇模板语法(重要!!!) 一.什么是模板? 只要是在html里面有模板语法就不是html文件了,这样的文件就叫做模板. 二.模板语法分类 一.模板语法之变量:语法为 {{ }}: ...
- Django进阶之缓存和信号
一.缓存 简介 由于Django是动态网站,所有每次请求均会去数据进行相应的操作,当程序访问量大时,耗时必然会更加明显,最简单解决方式是使用:缓存,缓存将一个某个views的返回值保存至内存或者mem ...
- mybatis一对一关联查询——(八)
1.需求 查询所有订单信息,关联查询下单用户信息. 注意: 因为一个订单信息只会是一个人下的订单,所以从查询订单信息出发关联查询用户信息为一对一查询.如果从用户信息出发查询用户下的订单信息则为一对多查 ...