题目链接:http://codeforces.com/problemset/problem/682/C

题意:如果点v在点u的子树上且dist(u,v)>a[v]则u和其整个子树都将被删去,求被删去的点数。

思路:1为根节点,从1开始DFS遍历,记录距离dis为到祖宗节点的最大距离。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e5+5;
int a[N],num[N],ans;
ll edge[N];
vector<int> G[N];
void dfs1(int x)
{
if(G[x].size()==0)
{
num[x]=1;
return;
}
for(int i=0;i<G[x].size();i++)
{
dfs1(G[x][i]);
num[x]+=num[G[x][i]];
}
num[x]++;
}
void dfs(int x,ll dis)
{
if(dis>a[x])
{
ans+=num[x];
return;
}
for(int i=0;i<G[x].size();i++)
dfs(G[x][i],max(dis+edge[G[x][i]],edge[G[x][i]]));//最大距离
}
int main()
{
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d",a+i);
for(int i=1;i<n;i++)
{
int v;
scanf("%d%I64d",&v,&edge[i+1]);
G[v].push_back(i+1);
}
dfs1(1);
dfs(1,0);
printf("%d\n",ans);
return 0;
}

codeforces 682C Alyona and the Tree(DFS)的更多相关文章

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

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

  2. 【CodeForces - 682C】Alyona and the Tree(dfs)

    Alyona and the Tree Descriptions 小灵决定节食,于是去森林里摘了些苹果.在那里,她意外地发现了一棵神奇的有根树,它的根在节点 1 上,每个节点和每条边上都有一个数字. ...

  3. Codeforces 682C Alyona and the Tree (树上DFS+DP)

    题目链接:http://codeforces.com/problemset/problem/682/C 题目大意:取树上任意一个点v,若点v的子树中有一个点u使得dist(v,u)>a[u]那么 ...

  4. CodeForces 682C Alyona and the Tree (树上DFS)

    题意:给定一棵树,每个叶子有一个权值,每条边也有一个权值,现在让你删最少的结点,使得从任何结点出发到另一个结点的边上权值和都小于两个结点的权值. 析:很明显是DFS,不过要想找出最少的结点可能不太容易 ...

  5. Codeforces 682C Alyona and the Tree(树形DP)

    题目大概说给一棵点有权.边也有权的树.一个结点v不高兴当且仅当存在一个其子树上的结点u,使得v到u路径上的边权和大于u的权值.现在要不断地删除叶子结点使得所有结点都高兴,问最少删几个叶子结点. 一开始 ...

  6. CodeForces 682C Alyona and the Tree(广搜 + 技巧)

    方法:从根节点开始广搜,如果遇到了应该删除的点,就再广搜删掉它的子树并标记,然后统计一下被标记的个数就是答案,所谓技巧就是从根节点开始搜索的时候,如果遇到了某个节点的距离<0,就让它是0,0可以 ...

  7. Codeforces E. Alyona and a tree(二分树上差分)

    题目描述: Alyona and a tree time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

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

  9. 2017ACM暑期多校联合训练 - Team 1 1003 HDU 6035 Colorful Tree (dfs)

    题目链接 Problem Description There is a tree with n nodes, each of which has a type of color represented ...

随机推荐

  1. sqlserver 纵横

    纵表转横表 create table Score ( Name ), Class ), score int ) ) ) ) ) ) ) ) select * from Score select t.N ...

  2. 快速排序(python版)

    #!coding:utf8 def quicksort(list_num, left, right): if left > right: return low = left high = rig ...

  3. BZOJ 3229: [Sdoi2008]石子合并

    3229: [Sdoi2008]石子合并 时间限制: 3 Sec  内存限制: 128 MB提交: 497  解决: 240[提交][][] 题目描述 在一个操场上摆放着一排N堆石子.现要将石子有次序 ...

  4. LTE Module User Documentation(翻译7)——无线环境地图(REM)、AMC 模型 和 CQI 计算

    LTE用户文档 (如有不当的地方,欢迎指正!) 12 Radio Environment Maps   通过使用类 RadioEnvironmentMapHelper  是可能输出文件 Radio E ...

  5. js执行顺序<转>

    JavaScript执行引擎并非一行一行地分析和执行程序,而是一段一段地分析执行的.而且在分析执行同一段代码中,定义式的函数语句会被提取出来优先执行.函数定义执行完后,才会按顺序执行其他代码. 先看看 ...

  6. IE6/7常用的hack

    hack基础: IE6: _selector{property:value;} selector{property:value;property:value !important;} //IE6 不支 ...

  7. jQuery Easing 使用方法及其图解

    jQuery Easing 使用方法及其图解,非常详尽:http://blog.sina.com.cn/s/blog_70a3539f0102v8az.html

  8. hdu 5358 First One

    题目链接:hdu 5358 思路不难理解,就是个尺取法而已,floor(log2X) + 1 就是求 X 的二进制表示的位数,对于题目来说这个值最多只是 30+,从这里入手开始枚举,运用尺取法可以达到 ...

  9. 主成分分析(PCA)核心思想

    参考链接:http://pinkyjie.com/2011/02/24/covariance-pca/ PCA的本质其实就是对角化协方差矩阵. PCA就是将高维的数据通过线性变换投影到低维空间上去,但 ...

  10. SPSS数据分析—协方差分析

    我们在实际工作中为了准确的分析问题,经常会收集多个变量,这些变量之前存在相互影响,导致分析的因素混杂,影响分析结果,为了获得准确的实验效应,我们需要控制其中一些影响因变量的变量,这些变量称为就协变量, ...