[AT2304] [agc010_c] Cleaning
题目链接
AtCoder:https://agc010.contest.atcoder.jp/tasks/agc010_c
洛谷:https://www.luogu.org/problemnew/show/AT2304
Solution
窝yy了好久的dpQAQ
标算很巧妙:我们考虑算每条边被经过了多少次,这里把一个叶子节点到另一个叶子节点的取石子看成\(\rm travel\)。
首先选一个度数大于一的点作为根节点,然后我们分类讨论:
- 每个叶子节点上面的边\(cnt\)一定等于当前点的石子数。
- 非叶子节点上面的边可以这样算:因为每个非叶子节点的进入的次数等于出去的次数,并且这个点被取完了,也就是说\(\sum _{v\in edge_x} cnt_v=2v[x]\),那么我们就可以确定了。
算完这个之后就可以判无解了:
- 如果根节点不满足\(\sum cnt =2v[x]\)就无解。
- 如果某条边的\(cnt\)为负就无解。
- 如果某条边的\(cnt\)大于两端的\(v[x]\)的任意一个就无解。
#include<bits/stdc++.h>
using namespace std;
void read(int &x) {
x=0;int f=1;char ch=getchar();
for(;!isdigit(ch);ch=getchar()) if(ch=='-') f=-f;
for(;isdigit(ch);ch=getchar()) x=x*10+ch-'0';x*=f;
}
void print(int x) {
if(x<0) putchar('-'),x=-x;
if(!x) return ;print(x/10),putchar(x%10+48);
}
void write(int x) {if(!x) putchar('0');else print(x);putchar('\n');}
#define lf double
#define ll long long
#define end puts("NO"),exit(0)
const int maxn = 1e5+10;
const int inf = 1e9;
const lf eps = 1e-8;
int head[maxn],tot=1,cnt[maxn<<1],n,v[maxn],d[maxn],rt;
struct edge{int to,nxt;}e[maxn<<1];
void add(int u,int vv) {e[++tot]=(edge){vv,head[u]},head[u]=tot,d[vv]++;}
void ins(int u,int vv) {add(u,vv),add(vv,u);}
void dfs(int x,int fa) {
if(d[x]==1) return cnt[head[x]]=cnt[head[x]^1]=v[x],void();
int rm=v[x]<<1,t=0;
for(int i=head[x];i;i=e[i].nxt)
if(e[i].to!=fa) dfs(e[i].to,x),rm-=cnt[i],cnt[i]>v[x]?end,0:0;
else t=i;
if(!t) return ;
if(rm>v[x]||rm<0) end;
cnt[t]=cnt[t^1]=rm;
}
int main() {
read(n);for(int i=1;i<=n;i++) read(v[i]);
if(n==2) puts(v[1]==v[2]?"YES":"NO"),exit(0);
for(int i=1,x,y;i<n;i++) read(x),read(y),ins(x,y);
for(int i=1;i<=n;i++) if(d[i]!=1) {rt=i,dfs(i,0);break;}
int t=0;for(int i=head[rt];i;i=e[i].nxt) t+=cnt[i];
if(t!=v[rt]<<1) end;else puts("YES");
return 0;
}
[AT2304] [agc010_c] Cleaning的更多相关文章
- 贪心/构造/DP 杂题选做Ⅲ
颓!颓!颓!(bushi 前传: 贪心/构造/DP 杂题选做 贪心/构造/DP 杂题选做Ⅱ 51. CF758E Broken Tree 讲个笑话,这道题是 11.3 模拟赛的 T2,模拟赛里那道题的 ...
- AT2304 Cleaning
AT2304 Cleaning 题意 一个树上每个节点有一些石子,每次只能选取两个叶子节点并将路径间的所有点上的石子数量减1,问是否能将所有石子取完. 思路 设 \(f_x\) 表示从 \(x\) 节 ...
- Atcoder Grand Contest 010 C - Cleaning 树贪心(伪)
C - Cleaning 题目连接: http://agc010.contest.atcoder.jp/tasks/agc010_c Description There is a tree with ...
- 【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 ...
随机推荐
- 有关Laravel 4 的 Homestead 安装部署的细节
对于Vagrant,我是相见恨晚的.有时候抽出几个小时的时间学会一种工具,对于将来可以节省几十甚至几百小时的时间. Vagant最大的好处就是节省了安装配置运行环境的时间,统一开发环境,同时可以最大限 ...
- Spark 序列化问题
在Spark应用开发中,很容易出现如下报错: org.apache.spark.SparkException: Task not serializable at org.apache.spark.ut ...
- 问题:python2.7 安装包失败,提示错误:Microsoft Visual C++ 9.0 is required (Unable to find vcvarsall.bat)
问题描述: 使用pip安装包时报错,error: Microsoft Visual C++ 9.0 is required (Unable to find vcvarsall.bat) 环境: pyt ...
- async+await 让界面飞,让双手爽
.net 4.5已经发布很久了,但是一直也没有静下心来好好的研究微软给开发者带来的喜悦. 今天我将简单的介绍下 async + await 这对搭档的出现,如何让频繁假死的界面飞起来(其实只是不再阻塞 ...
- 使用闭包的方式实现一个累加函数 addNum
使用闭包的方式实现一个累加函数 addNum,参数为 number 类型,每次返回的结果 = 上一次计算的值 + 传入的值,如: addNum(10); //10 addNum(12); //22 a ...
- Arduino语言
Arduino语言 Arduino语言是建立在C/C++基础上的,其实也就是基础的C语言,Arduino语言只不过把AVR单片机(微控制器)相关的一些参数设置都函数化,不用我们去了解他的底层,让我们不 ...
- 11-Dockerfile构建镜像
用 Dockerfile 创建上节的 ubuntu-with-vi,其内容则为: FROM ubuntu RUN apt-get update && apt-get install v ...
- http 502 bad gate way
世界杯期间,公司的cdn在回源时突然出现大量502. 刚出现问题时,因为考虑到一般502都是上游服务器出现问题,然后因为已经服务了很久都没有出现过这种问题, 就没有仔细考虑,就让回源中心的同事进行排查 ...
- MATLAB 笔记
MATLAB的学习 Matlab 主要有5大部分构成,分别是MATLAB语言,桌面工具与开发环境,数学函数库 ,图形系统和应用程序接口.以及众多的专业工具.
- Tree - XGBoost with parameter description
In the previous post, we talk about a very popular Boosting algorithm - Gradient Boosting Decision T ...