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,biN
  • 0≦Ai≦109
  • The given graph is a tree.

Input

The input is given from Standard Input in the following format:

N
A1 A2AN
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的更多相关文章

  1. 【bzoj1672】[USACO2005 Dec]Cleaning Shifts 清理牛棚

    题目描述 Farmer John's cows, pampered since birth, have reached new heights of fastidiousness. They now ...

  2. Coursera-Getting and Cleaning Data-week1-课程笔记

    博客总目录,记录学习R与数据分析的一切:http://www.cnblogs.com/weibaar/p/4507801.html -- Sunday, January 11, 2015 课程概述 G ...

  3. Coursera-Getting and Cleaning Data-Week2-课程笔记

    Coursera-Getting and Cleaning Data-Week2 Saturday, January 17, 2015 课程概述 week2主要是介绍从各个来源读取数据.包括MySql ...

  4. Coursera-Getting and Cleaning Data-Week3-dplyr+tidyr+lubridate的组合拳

    Coursera-Getting and Cleaning Data-Week3 Wednesday, February 04, 2015 好久不写笔记了,年底略忙.. Getting and Cle ...

  5. Coursera-Getting and Cleaning Data-week4-R语言中的正则表达式以及文本处理

    博客总目录:http://www.cnblogs.com/weibaar/p/4507801.html Thursday, January 29, 2015 补上第四周笔记,以及本次课程总结. 第四周 ...

  6. 【BZOJ1672】[Usaco2005 Dec]Cleaning Shifts 清理牛棚 动态规划

    [BZOJ1672][Usaco2005 Dec]Cleaning Shifts Description Farmer John's cows, pampered since birth, have ...

  7. poj 2376 Cleaning Shifts

    http://poj.org/problem?id=2376 Cleaning Shifts Time Limit: 1000MS   Memory Limit: 65536K Total Submi ...

  8. POJ 2376 Cleaning Shifts(轮班打扫)

    POJ 2376 Cleaning Shifts(轮班打扫) Time Limit: 1000MS   Memory Limit: 65536K [Description] [题目描述] Farmer ...

  9. POJ 2376 Cleaning Shifts 贪心

    Cleaning Shifts 题目连接: http://poj.org/problem?id=2376 Description Farmer John is assigning some of hi ...

  10. Bzoj 3389: [Usaco2004 Dec]Cleaning Shifts安排值班 最短路,神题

    3389: [Usaco2004 Dec]Cleaning Shifts安排值班 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 218  Solved: ...

随机推荐

  1. BasePath问题-nginx负载均衡配置

    在配置nginx+tomcat好后.将项目加入到webapps中.发现訪问主页时,css与js訪问不到,导致主页布局出错.细致分析原因后发现css与js的地址是basePath得出的.而basePat ...

  2. jxl 导入导出Excel(有模板)

    1.导入 @Override public String importBusinessScope(File file, String unit_id) throws Exception { Workb ...

  3. Openstack能解决这些问题吗?请各位大侠一起来讨论

    1.10万规模的虚拟机,每一个虚拟机能够在不论什么一个计算节点上启动,该怎样做?计算,存储,网络都是怎么拉通与配合的? 2.用户怎样自己定义业务网络,怎样解决网络不够用的情况?正常就4096个vlan ...

  4. 修复nexus4由于使用完美刷机或者刷机精灵导致的底层文件受损

    前一段nexus4升级android 4.4.4的时候突然发现无法使用线刷刷入官方镜像了.当时就感觉很奇怪,但是不知道怎么搞得刷进去了,但是第一次开机的时候总是卡在联网之后的验证(我当时以为是网络不好 ...

  5. Java-MyBatis:MyBatis

    ylbtech-Java-MyBatis:MyBatis 1.返回顶部 1. MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foun ...

  6. pandas把多个sheet读进一个DataFrame

    #!/usr/bin/python import pandas as pd import collections df_dict = pd.read_excel('c:\data\machines.x ...

  7. string 类型的翻转

    #include <string>#include <iostream>#include <stack> int main() { std::string str= ...

  8. android 从assets和res中读取文件(转)

    1. 相关文件夹介绍      在Android项目文件夹里面,主要的资源文件是放在res文件夹里面的.assets文件夹是存放不进行编译加工的原生文件,即该文件夹里面的文件不会像xml,java文件 ...

  9. PCB MVC启动顺序与各层之间数据传递对象关系

    准备着手基于MVC模式写一套Web端流程指示查看,先着手开发WebAPI打通数据接口,后续可扩展手机端 这里将MVC基本关系整理如下: 一.MVC启动顺序 二.MVC各层之间数据传递对象关系

  10. 日期数据类型为Date ,前台传递喂String的后台处理

    方法一: 在实体类里面将set方法里面将数据类型转为Date public void setBirth(String birth) { SimpleDateFormat sdf = new Simpl ...