Cleaning
Cleaning
Time limit : 2sec / Memory limit : 256MB
Score : 700 points
Problem Statement
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.
Constraints
- 2≦N≦105
- 1≦ai,bi≦N
- 0≦Ai≦109
- The given graph is a tree.
Input
The input is given from Standard Input in the following format:
N
A1 A2 … AN
a1 b1
:
aN−1 bN−1
Output
If it is possible to remove all the stones from the vertices, print YES
. Otherwise, print NO
.
Sample Input 1
5
1 2 1 1 2
2 4
5 2
3 2
1 3
Sample Output 1
YES
All the stones can be removed, as follows:
- Select vertices 4 and 5. Then, there is one stone remaining on each vertex except 4.
- Select vertices 1 and 5. Then, there is no stone on any vertex.
Sample Input 2
3
1 2 1
1 2
2 3
Sample Output 2
NO
Sample Input 3
6
3 2 2 2 2 2
1 2
2 3
1 4
1 5
4 6
Sample Output 3
YES
分析:考虑点与边的关系:
1.点为叶子节点,相邻的边权值为点权值;
2.点不为叶子,相邻的边权值和为点权值二倍;
这样dfs可以得出所有边权值;
以下几种情况不可行:
1.存在负权值;
2.与一个点相邻的边权值均不应大于点权值,否则不能两两分组;
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <bitset>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
#define pii pair<int,int>
#define sys system("pause")
const int maxn=1e5+;
using namespace std;
inline ll gcd(ll p,ll q){return q==?p:gcd(q,p%q);}
inline ll qpow(ll p,ll q){ll f=;while(q){if(q&)f=f*p;p=p*p;q>>=;}return f;}
inline void umax(ll &p,ll q){if(p<q)p=q;}
inline void umin(ll &p,ll q){if(p>q)p=q;}
inline ll read()
{
ll x=;int f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int n,m,k,t,a[maxn];
ll cnt[maxn];
vi e[maxn];
bool flag=true;
void dfs(int x,int y)
{
if(!flag)return;
ll p=;
for(int z:e[x])
{
if(z==y)continue;
dfs(z,x);
if(cnt[z]>a[x])
{
flag=false;
return;
}
p+=cnt[z];
}
if((int)e[x].size()==)
{
cnt[x]=a[x];
}
else
{
cnt[x]=(ll)*a[x]-p;
if(cnt[x]<||cnt[x]>a[x])
{
flag=false;
return;
}
}
}
int main()
{
int i,j;
scanf("%d",&n);
rep(i,,n)scanf("%d",&a[i]);
rep(i,,n-)scanf("%d%d",&j,&k),e[j].pb(k),e[k].pb(j);
if(n==)
{
puts(a[]==a[]?"YES":"NO");
return ;
}
rep(i,,n)
{
if((int)e[i].size()>)
{
dfs(i,-);
if(!flag)puts("NO");
else if(cnt[i]!=)puts("NO");
else puts("YES");
return ;
}
}
return ;
}
Cleaning的更多相关文章
- 【bzoj1672】[USACO2005 Dec]Cleaning Shifts 清理牛棚
题目描述 Farmer John's cows, pampered since birth, have reached new heights of fastidiousness. They now ...
- Coursera-Getting and Cleaning Data-week1-课程笔记
博客总目录,记录学习R与数据分析的一切:http://www.cnblogs.com/weibaar/p/4507801.html -- Sunday, January 11, 2015 课程概述 G ...
- Coursera-Getting and Cleaning Data-Week2-课程笔记
Coursera-Getting and Cleaning Data-Week2 Saturday, January 17, 2015 课程概述 week2主要是介绍从各个来源读取数据.包括MySql ...
- Coursera-Getting and Cleaning Data-Week3-dplyr+tidyr+lubridate的组合拳
Coursera-Getting and Cleaning Data-Week3 Wednesday, February 04, 2015 好久不写笔记了,年底略忙.. Getting and Cle ...
- Coursera-Getting and Cleaning Data-week4-R语言中的正则表达式以及文本处理
博客总目录:http://www.cnblogs.com/weibaar/p/4507801.html Thursday, January 29, 2015 补上第四周笔记,以及本次课程总结. 第四周 ...
- 【BZOJ1672】[Usaco2005 Dec]Cleaning Shifts 清理牛棚 动态规划
[BZOJ1672][Usaco2005 Dec]Cleaning Shifts Description Farmer John's cows, pampered since birth, have ...
- poj 2376 Cleaning Shifts
http://poj.org/problem?id=2376 Cleaning Shifts Time Limit: 1000MS Memory Limit: 65536K Total Submi ...
- POJ 2376 Cleaning Shifts(轮班打扫)
POJ 2376 Cleaning Shifts(轮班打扫) Time Limit: 1000MS Memory Limit: 65536K [Description] [题目描述] Farmer ...
- POJ 2376 Cleaning Shifts 贪心
Cleaning Shifts 题目连接: http://poj.org/problem?id=2376 Description Farmer John is assigning some of hi ...
- Bzoj 3389: [Usaco2004 Dec]Cleaning Shifts安排值班 最短路,神题
3389: [Usaco2004 Dec]Cleaning Shifts安排值班 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 218 Solved: ...
随机推荐
- C# 读取ini文件 百度问问学习文档
C# 读取ini文件 10 有多个section,现想读取整个ini文件和指定section下所有内容 补充: 发布答案可以,请对准题目啊,我不要指定节点的内容,我知道!我要的是读取指定区域的内容,假 ...
- EOJ 3194 字符串消除
给定一个由大写字母’A’.’B’.’C’构成的字符串s,按如下进行消除过程: 1.字符串s中连续相同字母组成的子串,如果子串的长度大于1,那么这些子串会被同时消除,余下的字符拼成新的字符串. 例如:” ...
- Appium + python - automator定位升级版操作
# coding:utf-8 """参考博客链接:https://www.cnblogs.com/yoyoketang/p/7843819.html"" ...
- MVVM实现ViewModel获取View输入验证状态
由于Binding只把Convert成功的值送往Source,当目标中的值Convert失败时Source的值依然是旧值,所以ViewModel必须获取View的输入验证状态,以下是本人的实现. 当“ ...
- webapi时间字段返回格式设置及返回model首字母小写
GlobalConfiguration.Configuration.Formatters.Remove(new XmlMediaTypeFormatter()); // 解决json序列化时的循环引用 ...
- 设计模式之合成模式(Java语言描述)
<JAVA与模式>一书中开头是这样描述合成(Composite)模式的: 合成模式属于对象的结构模式,有时又叫做"部分--整体"模式.合成模式将对象组织到树结构中,可以 ...
- set statistics profile on实例
set statistics profile on实例 1.SQL语句实例 SQL 代码 复制 SET STATISTICS PROFILE ON GO SELECT COUNT(b.[Sal ...
- 【Oracle】 手工建库
操作系统:OEL 5.6 数据库版本:Oracle11gR2 11.2.0.4.0 新建数据库名称:lgr 1 生成pfile和口令文件 1)生成pfile文件,在模板文件init.ora中提取 [ ...
- MSP430之section(1)
1 Intro The smallest unit of an object file is a section. A section is a block of code or data that ...
- C++编译错误fatal error C1004: 发现意外的文件尾
出现这种情况就是类或者结构体的定义后面没有加“;”导致的. 而且这种问题好难排查.