题解 CF682C 【Alyona and the Tree】
简单搜索题,我们每找到一组不满足题目给出条件的点和边就将其整个子树删除,然后最终答案加上该子树的大小即可。注意,搜索的时候如果当前的边权和sum已经为负了,应该将其改为0(可以想想为什么)
注:题目翻译有误
原文中的小于应该改为小于等于
#include<bits/stdc++.h>
#define maxn 100005
using namespace std;
inline char get(){
static char buf[30000],*p1=buf,*p2=buf;
return p1==p2 && (p2=(p1=buf)+fread(buf,1,30000,stdin),p1==p2)?EOF:*p1++;
}
inline int read(){
register char c=get();register int f=1,_=0;
while(c>'9' || c<'0')f=(c=='-')?-1:1,c=get();
while(c<='9' && c>='0')_=(_<<3)+(_<<1)+(c^48),c=get();
return _*f;
}
struct edge{
int u,v,w,next;
}E[maxn<<1];
int p[maxn],eid;
inline void init(){
for(register int i=0;i<maxn;i++)p[i]=-1;
eid=0;
}
inline void insert(int u,int v,int w){
E[eid].u=u;
E[eid].v=v;
E[eid].w=w;
E[eid].next=p[u];
p[u]=eid++;
}
inline void insert2(int u,int v,int w){
insert(u,v,w);
insert(v,u,w);
}
int n;
int a[maxn];
int fa[maxn],size[maxn];
int ans=0;
inline void get_size(int u,int fa){
size[u]=1;
for(register int i=p[u];~i;i=E[i].next){
int v=E[i].v;
if(v==fa)continue;
get_size(v,u);
size[u]+=size[v];
}
}
inline void dfs(int u,int fa,int sum){
if(sum>a[u]){
ans+=size[u];
//cout<<sum<<" "<<a[u]<<endl;
return;
}
for(register int i=p[u];~i;i=E[i].next){
int v=E[i].v;
int w=E[i].w;
if(v==fa)continue;
dfs(v,u,max(sum+w,0));
}
}
int main(){
//freopen("1.txt","r",stdin);
init();
n=read();
for(register int i=1;i<=n;i++)a[i]=read();
fa[1]=-1;
for(register int i=2;i<=n;i++){
int p=read(),c=read();
fa[i]=p;
insert2(i,p,c);
}
get_size(1,-1);
dfs(1,-1,0);
cout<<ans<<endl;
return 0;
}
题解 CF682C 【Alyona and the Tree】的更多相关文章
- CF682C Alyona and the Tree
题意翻译 题目描述 给你一棵树,边与节点都有权值,根节点为1,现不停删除叶子节点形成新树,问最少删掉几个点,能使得最后剩下的树内,∀v与其子树内∀u间边权的和小于点u权值 输入输出格式 输入格式: 第 ...
- Codeforces Round #381 (Div. 2)D. Alyona and a tree(树+二分+dfs)
D. Alyona and a tree Problem Description: Alyona has a tree with n vertices. The root of the tree is ...
- Codeforces Round #381 (Div. 1) B. Alyona and a tree dfs序 二分 前缀和
B. Alyona and a tree 题目连接: http://codeforces.com/contest/739/problem/B Description Alyona has a tree ...
- Codeforces Round #381 (Div. 2) D. Alyona and a tree 树上二分+前缀和思想
题目链接: http://codeforces.com/contest/740/problem/D D. Alyona and a tree time limit per test2 secondsm ...
- CodeForces 682C Alyona and the Tree (树+dfs)
Alyona and the Tree 题目链接: http://acm.hust.edu.cn/vjudge/contest/121333#problem/C Description Alyona ...
- Codeforces Round #358 (Div. 2) C. Alyona and the Tree 水题
C. Alyona and the Tree 题目连接: http://www.codeforces.com/contest/682/problem/C Description Alyona deci ...
- XJOI 3363 树4/ Codeforces 739B Alyona and a tree(树上差分+路径倍增)
D. Alyona and a tree time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
- 【30.36%】【codeforces 740D】Alyona and a tree
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- codeforces 381 D Alyona and a tree(倍增)(前缀数组)
Alyona and a tree time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- Alyona and a tree
Alyona and a tree time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
随机推荐
- MongoDB的角色作用(2)
数据压力大到机器支撑不了的时候能否做到自动扩展? 在系统早期,数据量还小的时候不会引起太大的问题,但是随着数据量持续增多,后续迟早会出现一台机器硬件瓶颈问题的.而MongoDB主打的就是海量数据架构, ...
- iOS之序列化与反序列化
所谓的序列化和反序列化就是将数据结构或对象和二进制串之间相互转换的过程: 本人的理解是当你于写数据需要本地存储时,即将你的数据写到硬盘上的时候,你就必须对他进行序列化,转换成二进制文件,从而便于在磁盘 ...
- 【luogu P2195 HXY造公园】 题解
题目链接:https://www.luogu.org/problemnew/show/P2195 fir.吐槽题目(省略1w字 sec.考虑对一个森林的维护,每棵树用并查集维护. 操作1:输出当前查询 ...
- FreeRTOS 查询任务 剩余的栈空间的 方法
FreeRTOS 源码下载地址 1.官方文档提供了 函数 用来查询 任务 剩余 栈 空间,首先是看官方的文档解释(某位大神 翻译 的 官方文档.) 参数解释: xTask:被查询任 ...
- Relay GraphQL理解
Relay是构建数据驱动 React 应用的js框架. Relay:特性一.声明式:不再使用一个命令式API与数据存储通讯.而是简单的使用RraphQL声明组件数据需求,让Relay理解如何及什么时候 ...
- MySql第几行到第几行语句
1.查询第一行记录: select * from table limit 1 2.查询第n行到第m行记录 select * from table1 limit n-1,m-n; SELECT * FR ...
- Oracle 11g密码过期问题
Oracle 11g默认用户密码会在使用180天后过期,我们可以通过dba_users数据字典看一下用户的信息. SQL> select username,account_status,lock ...
- 解决Windows10下小娜无法搜索本地应用的问题
适用场景 小娜突然出现各种问题.比如突然无法搜索到本地应用...等其它问题 一般使用下面的方法,将小娜进行重新注册就ok了. 解决方案 1.用管理员权限打开 C:\Windows\System32\W ...
- ARC下IBOutlet用weak还是strong
原文来自这里. 今天用Xcode5的时候,发现默认的IBoutlet的属性设置为weak——因为Xcode5建立的工程都是ARC的了.但是当时还有点不明白,因为项目的原因,一直没有正式使用过ARC.于 ...
- Linux中Zookeeper部署和集群部署
自己网上下载安装包,我下载的是tar.gz安装包直接解压,也可以下载rpm格式 1.下载zookeeper安装包,放到/usr/local/zookeeper安装包网上下载 2.解压文件tar -zx ...