简单搜索题,我们每找到一组不满足题目给出条件的点和边就将其整个子树删除,然后最终答案加上该子树的大小即可。注意,搜索的时候如果当前的边权和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】的更多相关文章

  1. CF682C Alyona and the Tree

    题意翻译 题目描述 给你一棵树,边与节点都有权值,根节点为1,现不停删除叶子节点形成新树,问最少删掉几个点,能使得最后剩下的树内,∀v与其子树内∀u间边权的和小于点u权值 输入输出格式 输入格式: 第 ...

  2. 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 ...

  3. 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 ...

  4. 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 ...

  5. CodeForces 682C Alyona and the Tree (树+dfs)

    Alyona and the Tree 题目链接: http://acm.hust.edu.cn/vjudge/contest/121333#problem/C Description Alyona ...

  6. 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 ...

  7. 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 ...

  8. 【30.36%】【codeforces 740D】Alyona and a tree

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  9. 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 ...

  10. Alyona and a tree

    Alyona and a tree time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

随机推荐

  1. 记录 iOS 各种跳转到系统应用

    MARK ----拨打电话 NSString* phoneVersion = [[UIDevice currentDevice] systemVersion]; if (phoneVersion.fl ...

  2. 十二、IntelliJ IDEA 中的版本控制介绍(中)

    由于 IntelliJ IDEA 支持的版本控制工具非常的多,但咱们真正能够用到的也就两三个而已,因此在本篇博文中,咱们主要介绍 SVN.Git 和 GitHub 的配置方法. SVN 如果想要在 I ...

  3. Spring知识点小结(二)

    一.配置非自定义的Bean(数据源DataSource模型) DBCP数据源:        导入dbcp的jar包:dbcp+pool+connector                代码实现:  ...

  4. 常见web漏洞

    常见的web漏洞——文件上传漏洞 一.文件上传漏洞概述    文件上传漏洞是指用户上传了一个可执行的脚本文件,并通过此脚本文件获得了执行服务器端命令的能力.这种攻击方式是最为直接和有效的,有时候几乎没 ...

  5. 用 crontab 实现开机自动运行脚本

    开发「bufpay.com 个人即时到账收款平台」的时候,订单状态和支付二维码的状态如果过期了要实时修改状态,最大效率利用支付二维码. 过期脚本需要开机启动,并且 deamon 运行,有很多办法可以开 ...

  6. C++ 内存、new与malloc分配内存区别?

    一关于内存 1.内存分配方式 内存分配方式有三种: (1)从静态存储区域分配.内存在程序编译的时候就已经分配好,这块内存在程序的整个运行期间都存在.例如全局变量,static变量. (2)在栈上创建. ...

  7. Oracle之基础操作

    sqlplus常用命令: 进入sqlplus模式:sqlplus /nolog 管理员登录: conn / as sysdba 登录本机的数据库 conn sys/123456 as sysdba 普 ...

  8. hadoop生态搭建(3节点)-16.elk配置

    # ==================================================================ELK环境准备 # 修改文件限制 # * 代表Linux所有用户 ...

  9. python学习——单例模式

    在python中,单例模式在面试中非常重要.下面来给大家推荐一个Python中实现单例模式的博客地址. https://www.cnblogs.com/huchong/p/8244279.html

  10. Python学习笔记八:文件操作(续),文件编码与解码,函数,递归,函数式编程介绍,高阶函数

    文件操作(续) 获得文件句柄位置,f.tell(),从0开始,按字符数计数 f.read(5),读取5个字符 返回文件句柄到某位置,f.seek(0) 文件在编辑过程中改变编码,f.detech() ...