简单搜索题,我们每找到一组不满足题目给出条件的点和边就将其整个子树删除,然后最终答案加上该子树的大小即可。注意,搜索的时候如果当前的边权和sum已经为负了,应该将其改为0(可以想想为什么)

注:题目翻译有误

原文中的小于应该改为小于等于

  1. #include<bits/stdc++.h>
  2. #define maxn 100005
  3. using namespace std;
  4. inline char get(){
  5. static char buf[30000],*p1=buf,*p2=buf;
  6. return p1==p2 && (p2=(p1=buf)+fread(buf,1,30000,stdin),p1==p2)?EOF:*p1++;
  7. }
  8. inline int read(){
  9. register char c=get();register int f=1,_=0;
  10. while(c>'9' || c<'0')f=(c=='-')?-1:1,c=get();
  11. while(c<='9' && c>='0')_=(_<<3)+(_<<1)+(c^48),c=get();
  12. return _*f;
  13. }
  14. struct edge{
  15. int u,v,w,next;
  16. }E[maxn<<1];
  17. int p[maxn],eid;
  18. inline void init(){
  19. for(register int i=0;i<maxn;i++)p[i]=-1;
  20. eid=0;
  21. }
  22. inline void insert(int u,int v,int w){
  23. E[eid].u=u;
  24. E[eid].v=v;
  25. E[eid].w=w;
  26. E[eid].next=p[u];
  27. p[u]=eid++;
  28. }
  29. inline void insert2(int u,int v,int w){
  30. insert(u,v,w);
  31. insert(v,u,w);
  32. }
  33. int n;
  34. int a[maxn];
  35. int fa[maxn],size[maxn];
  36. int ans=0;
  37. inline void get_size(int u,int fa){
  38. size[u]=1;
  39. for(register int i=p[u];~i;i=E[i].next){
  40. int v=E[i].v;
  41. if(v==fa)continue;
  42. get_size(v,u);
  43. size[u]+=size[v];
  44. }
  45. }
  46. inline void dfs(int u,int fa,int sum){
  47. if(sum>a[u]){
  48. ans+=size[u];
  49. //cout<<sum<<" "<<a[u]<<endl;
  50. return;
  51. }
  52. for(register int i=p[u];~i;i=E[i].next){
  53. int v=E[i].v;
  54. int w=E[i].w;
  55. if(v==fa)continue;
  56. dfs(v,u,max(sum+w,0));
  57. }
  58. }
  59. int main(){
  60. //freopen("1.txt","r",stdin);
  61. init();
  62. n=read();
  63. for(register int i=1;i<=n;i++)a[i]=read();
  64. fa[1]=-1;
  65. for(register int i=2;i<=n;i++){
  66. int p=read(),c=read();
  67. fa[i]=p;
  68. insert2(i,p,c);
  69. }
  70. get_size(1,-1);
  71. dfs(1,-1,0);
  72. cout<<ans<<endl;
  73. return 0;
  74. }

题解 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. 设计模式——Spirng_Bean工厂

    前言:对于简单工厂和抽象工厂都有自己的优点和缺点, 比如简单工厂,如果你需要实现的类过多,你最后会出现工厂泛滥的情况,没有有效的控制代码的可重复性.http://www.cnblogs.com/xia ...

  2. 去掉CodeIgniter URL中的index.php

    CI默认的rewrite url中是类似这样的,例如你的CI根目录是在/CodeIgniter/下,你的下面的二级url就类似这样http://localhost /CodeIgniter/index ...

  3. Lodash数组篇

    概念简述 lodash 是一个类库 Lodash 通过降低 array.number.objects.string 等等的使用难度从而让 JavaScript 变得更简单 用法  let _ = re ...

  4. .net core运行环境搭建 linux + windows

    ---------------------------------------linux------------------------------------------------- 一.添加do ...

  5. oracle查询相关注意点

    单表查询: .or 和 and 混合使用 需求:查询业主名称包含'刘'或门牌号包含'5'的,并且地址编号为3的记录 and 的权限优先于 or 所以需要在or的两边添加() 2. 范围查询 除了传统的 ...

  6. Oracle行转列,pivot函数和unpivot函数

    pivot函数:行转列函数: 语法:pivot(任一聚合函数 for 需专列的值所在列名 in (需转为列名的值)):unpivot函数:列转行函数: 语法:unpivot(新增值所在列的列名 for ...

  7. Extjs 环境安装【转】

    Sencha Cmd安装 在使用前端框架ExtJs前,需对其环境加以部署,对自己的安装过程加以记录. 建议大家在安装时,最好使用VPN,个中缘由,生在跳槽的程序袁应该都懂吧.不然ruby相关命令执行时 ...

  8. easyui 上 datagrid 的表头的checkbox全选时 取消选中 disabled的checkbox

    业务需求: 正常情况下,easyui的全选checkbox会选择表中全部的checkbox包括行.及时对checkbox加了disable属性也没有效果.但是现在的业务是当对checkbox加了dis ...

  9. 682. Baseball Game (5月28日)

    解答(打败98.60%) class Solution { public: int calPoints(vector<string>& ops) { vector<int&g ...

  10. Docker 相关命令汇总

    操作容器的命令 镜像中的容器启动之后可以在 docker 中操作和查看容器的信息 l   docker ps 查看运行的容器,如果想查看全部加上参数-a 即可 l   docker create 完整 ...