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: ...
随机推荐
- Mybatis 框架文档 超具体笔记
1 Mybatis入门 1.1 单独使用jdbc编程问题总结 1.1.1 jdbc程序 Public static void main(String[] args) { Connec ...
- android Service not registered
Caused by: java.lang.IllegalArgumentException: Service not registered:com.broadcom.bt.app.settings.S ...
- Android源码编译全过程记录(基于最新安卓5.1.0)【转】
本文转载自:http://blog.csdn.net/drg1612/article/details/44802533 我的编译条件: 1 Ubuntu Kylin 14.04 长期支持版 下载地址 ...
- C# 实现DataGridView分页功能
C#实现DataGridView分页功能 2010-07-17 13:45:42| 分类: C#|字号 订阅 从界面可以看到,在设计时需要一个DataGridView.BindingNavi ...
- 电脑升级win10后visio的问题
上周由于电脑意外蓝屏,系统从win7升级到了win10,昨天工作写文档时才发现缺少画图的工具,于是按照了visio2013,在编辑设计图时发现,一旦用visio打开或编辑图后再到word里设计图的内容 ...
- SpringMVC中url映射到Controller
SpringMVC也是一种基于请求驱动的WEB框架,并且使用了前端控制器的设计模式.前端控制器就是DispatcherServlet控制器,只要满足web.xml文件中的[url-pattern]的规 ...
- [转]浏览器缓存详解: expires, cache-control, last-modified, etag详细说明
最近在对CDN进行优化,对浏览器缓存深入研究了一下,记录一下,方便后来者 画了一个草图: 每个状态的详细说明如下: 1.Last-Modified 在浏览器第一次请求某一个URL时,服务器端的返回状态 ...
- 关于MYSQL 存储过程的文章摘录
1. 存储过程简介 我们常用的操作数据库语言SQL语句在执行的时候需要要先编译,然后执行,而存储过程(Stored Procedure)是一组为了完成特定功能的SQL语句集,经编译后存储 ...
- dell inspiron 15 3000 装XP win7 等GHOST系统方法
dell inspiron 装XP win7 等GHOST系统方法 . 开机按F2,进入BIOS .在 BIOS 的Boot菜单下,将Secure Boot 改为 Disabled . 将Boot L ...
- DNN:逻辑回归与 SoftMax 回归方法
UFLDL Tutorial 翻译系列:http://deeplearning.stanford.edu/wiki/index.php/UFLDL_Tutorial 第四章:SoftMax回归 简介: ...