题解 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 ...
随机推荐
- 设计模式——Spirng_Bean工厂
前言:对于简单工厂和抽象工厂都有自己的优点和缺点, 比如简单工厂,如果你需要实现的类过多,你最后会出现工厂泛滥的情况,没有有效的控制代码的可重复性.http://www.cnblogs.com/xia ...
- 去掉CodeIgniter URL中的index.php
CI默认的rewrite url中是类似这样的,例如你的CI根目录是在/CodeIgniter/下,你的下面的二级url就类似这样http://localhost /CodeIgniter/index ...
- Lodash数组篇
概念简述 lodash 是一个类库 Lodash 通过降低 array.number.objects.string 等等的使用难度从而让 JavaScript 变得更简单 用法 let _ = re ...
- .net core运行环境搭建 linux + windows
---------------------------------------linux------------------------------------------------- 一.添加do ...
- oracle查询相关注意点
单表查询: .or 和 and 混合使用 需求:查询业主名称包含'刘'或门牌号包含'5'的,并且地址编号为3的记录 and 的权限优先于 or 所以需要在or的两边添加() 2. 范围查询 除了传统的 ...
- Oracle行转列,pivot函数和unpivot函数
pivot函数:行转列函数: 语法:pivot(任一聚合函数 for 需专列的值所在列名 in (需转为列名的值)):unpivot函数:列转行函数: 语法:unpivot(新增值所在列的列名 for ...
- Extjs 环境安装【转】
Sencha Cmd安装 在使用前端框架ExtJs前,需对其环境加以部署,对自己的安装过程加以记录. 建议大家在安装时,最好使用VPN,个中缘由,生在跳槽的程序袁应该都懂吧.不然ruby相关命令执行时 ...
- easyui 上 datagrid 的表头的checkbox全选时 取消选中 disabled的checkbox
业务需求: 正常情况下,easyui的全选checkbox会选择表中全部的checkbox包括行.及时对checkbox加了disable属性也没有效果.但是现在的业务是当对checkbox加了dis ...
- 682. Baseball Game (5月28日)
解答(打败98.60%) class Solution { public: int calPoints(vector<string>& ops) { vector<int&g ...
- Docker 相关命令汇总
操作容器的命令 镜像中的容器启动之后可以在 docker 中操作和查看容器的信息 l docker ps 查看运行的容器,如果想查看全部加上参数-a 即可 l docker create 完整 ...