题目链接: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. Unity-Animator在Editor状态下的单个/批量预览工具

    网上有一个版本,但是调了半天用不了.于是自己动手写了一个 单个预览 批量预览 使用: 下载地址: http://files.cnblogs.com/files/hont/AnimatorClipPre ...

  2. Unity3D之协程(Coroutines & Yield )

    在Unity中StartCoroutine/yield return这个模式到底是怎么应用的? 比如你要一个方法进行一个比较耗时的复杂运算~同时又想让脚本流畅的进行其他操作而不是卡在那里等该方法执行完 ...

  3. CentOS 7 最小化安装的网络配置

    默认的最小化安装CentOS 7系统以后,是没有ipconfig这个命令的,依赖于net-tools工具包. 一.nmtui 这是一个类似于图形化的命令(和setup类似) 通过这个组件窗口可以设置各 ...

  4. CentOS6.5 安装 tomcat

    1.首先下载tomcat.tar.gz包 http://tomcat.apache.org/download-70.cgi 2.移动到目标文件夹 mv tomcat.tar.gz /usr/tomca ...

  5. python pymongo-ensure_index

    create_index 与 ensure_index 一.概述 ensure_index() 在mongoDB中创建索引 看了[1]感觉自己有些断章取义 ----------41316-- 参考链接 ...

  6. (转)linux grep 正则表达式

    转自:http://www.cnblogs.com/xiaouisme/archive/2012/11/09/2762543.html -------------------------------- ...

  7. 20161026__Oracle10g_DataGuard

    1. orcl.__db_cache_size=180355072 orcl.__java_pool_size=4194304 orcl.__large_pool_size=4194304 orcl. ...

  8. c++ 在windows下建立目录

    #include <direct.h> #include <stdlib.h> #include <stdio.h> int main( void ) { ) { ...

  9. html中button自动提交表单?

    在ie中,button默认的type是button,而其他浏览器和W3C标准中button默认的属性都是submit

  10. Nmap 使用指南

    Namp的英文全称是“Network Mapper”,Namp可用来快速扫描大型网络或是单个主机 nmap主要包括四个方面  主机发现.端口扫描.应用与版本侦测.操作系统侦测 主机发现原理:主机发现的 ...