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. Middle-help 终极实现元素水平垂直居中

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...

  2. CNN tensorflow text classification CNN文本分类的例子

    from:http://deeplearning.lipingyang.org/tensorflow-examples-text/ TensorFlow examples (text-based) T ...

  3. 85.Ext.Window

    转自:https://chenjumin.iteye.com/blog/668421 1.主要配置项:       closable:是否允许关闭窗口,默认为true.       closeActi ...

  4. Eqs(枚举+ hash)

    http://poj.org/problem?id=1840 题意:给出系数a1,a2,a3,a4,a5,求满足方程的解有多少组. 思路:有a1x13+ a2x23+ a3x33+ a4x43+ a5 ...

  5. 在linux服务器centos上使用svn同步代码到项目中

    一.需求 1.在多人开发过程中代码的管理以及版本的控制是一个很重要的问题,因为在开发过程中我们可能会同时更改过某个文件或者更改过多个文件, 这会导致我们很容易发生错误.所以我们需要一个方式去管理我们的 ...

  6. 安装DotnetCore-Vue项目模板

    dotnet new --install Microsoft.AspNetCore.SpaTemplates::*

  7. itext 生成doc文档 小结(自己备忘)

    1.引入maven <dependency> <groupId>com.lowagie</groupId> <artifactId>itext</ ...

  8. 消除svn选定(checkout)桌面上文件显示一大堆问号。

    图片: 解决方法一: 桌面右键选择TortoiseSVN——>点击Settings,如下图,选中Icon Overlays(图标覆盖),去勾选Fixed drives(本地磁盘),点击确定,按F ...

  9. windows服务安装错误 在‘安装’过程发生异常:System.ComponentModel.Win32Exception:系统正在关机

    今天安装windows服务的时候先是在本地安装测试通过,但是一到服务器就一直安装失败 在‘安装’过程发生异常:System.ComponentModel.Win32Exception:系统正在关机 然 ...

  10. nodejs脚手架express-generator

    1.安装生成器 npm install express-generator -g 2. 创建名称为APP的应用: express my-project 3.安装依赖包 cd my-project np ...