CodeForces 682C Alyona and the Tree (树+dfs)
Alyona and the Tree
题目链接:
http://acm.hust.edu.cn/vjudge/contest/121333#problem/C
Description
Alyona decided to go on a diet and went to the forest to get some apples. There she unexpectedly found a magic rooted tree with root in the vertex 1, every vertex and every edge of which has a number written on.
The girl noticed that some of the tree's vertices are sad, so she decided to play with them. Let's call vertex vsad if there is a vertex u in subtree of vertex v such that dist(v, u) > au, where au is the number written on vertex u, dist(v, u) is the sum of the numbers written on the edges on the path from v to u.
Leaves of a tree are vertices connected to a single vertex by a single edge, but the root of a tree is a leaf if and only if the tree consists of a single vertex — root.
Thus Alyona decided to remove some of tree leaves until there will be no any sad vertex left in the tree. What is the minimum number of leaves Alyona needs to remove?
Input
In the first line of the input integer n (1 ≤ n ≤ 105) is given — the number of vertices in the tree.
In the second line the sequence of n integers a1, a2, ..., an (1 ≤ ai ≤ 109) is given, where ai is the number written on vertex i.
The next n - 1 lines describe tree edges: ith of them consists of two integers pi and ci(1 ≤ pi ≤ n, - 109 ≤ ci ≤ 109), meaning that there is an edge connecting vertices i + 1 and pi with number ci written on it.
Output
Print the only integer — the minimum number of leaves Alyona needs to remove such that there will be no any sad vertex left in the tree.
Sample Input
Input
9
88 22 83 14 95 91 98 53 11
3 24
7 -8
1 67
1 64
9 65
5 12
6 -80
3 8
Output
5
题意:
删除最少的叶节点,使得不存在祖先路径之和大于点权的点;
题解:
直接dfs判断每个点是否要被删除,找出所有不需要删除的点;
WA:路径权和为负时应置零----因为判断的是所有祖先点到该点的路径.
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <vector>
#define LL long long
#define eps 1e-8
#define maxn 150000
#define inf 0x3f3f3f3f
#define IN freopen("in.txt","r",stdin);
using namespace std;
typedef pair<int,LL> pii;
vector<pii> g[maxn];
int n;
LL num[maxn];
bool vis[maxn];
int ans;
void dfs(int u, LL sum) {
vis[u] = 1;
if(sum <= num[u]) ans++;
else return;
int sz = g[u].size();
for(int i=0; i<sz; i++) {
if(vis[g[u][i].first]) continue;
dfs(g[u][i].first, max(0LL,sum+g[u][i].second));
}
}
int main(int argc, char const *argv[])
{
//IN;
while(scanf("%d",&n) != EOF)
{
memset(vis, 0, sizeof(vis));
for(int i=1; i<=n; i++) g[i].clear();
for(int i=1; i<=n; i++) scanf("%I64d", &num[i]);
for(int i=1; i<=n-1; i++) {
int v;LL w; scanf("%d %I64d",&v,&w);
g[v].push_back(make_pair(i+1,w));
g[i+1].push_back(make_pair(v,w));
}
ans = 0LL;
dfs(1, 0LL);
printf("%d\n", n-ans);
}
return 0;
}
CodeForces 682C Alyona and the Tree (树+dfs)的更多相关文章
- codeforces 682C Alyona and the Tree(DFS)
题目链接:http://codeforces.com/problemset/problem/682/C 题意:如果点v在点u的子树上且dist(u,v)>a[v]则u和其整个子树都将被删去,求被 ...
- XJOI3363 树3/Codeforces 682C Alyona and the Tree(dfs)
Alyona decided to go on a diet and went to the forest to get some apples. There she unexpectedly fou ...
- Codeforces 682C Alyona and the Tree (树上DFS+DP)
题目链接:http://codeforces.com/problemset/problem/682/C 题目大意:取树上任意一个点v,若点v的子树中有一个点u使得dist(v,u)>a[u]那么 ...
- CodeForces 682C Alyona and the Tree (树上DFS)
题意:给定一棵树,每个叶子有一个权值,每条边也有一个权值,现在让你删最少的结点,使得从任何结点出发到另一个结点的边上权值和都小于两个结点的权值. 析:很明显是DFS,不过要想找出最少的结点可能不太容易 ...
- codeforces 682C Alyona and the Tree DFS
这个题就是在dfs的过程中记录到根的前缀和,以及前缀和的最小值 #include <cstdio> #include <iostream> #include <ctime ...
- Codeforces 682C Alyona and the Tree(树形DP)
题目大概说给一棵点有权.边也有权的树.一个结点v不高兴当且仅当存在一个其子树上的结点u,使得v到u路径上的边权和大于u的权值.现在要不断地删除叶子结点使得所有结点都高兴,问最少删几个叶子结点. 一开始 ...
- Codeforces 682C Alyona and the Tree
题目链接:http://codeforces.com/problemset/problem/682/C 分析:存图,用dfs跑一遍,详细见注释 1 #include<iostream> 2 ...
- CodeForces 682C Alyona and the Tree(广搜 + 技巧)
方法:从根节点开始广搜,如果遇到了应该删除的点,就再广搜删掉它的子树并标记,然后统计一下被标记的个数就是答案,所谓技巧就是从根节点开始搜索的时候,如果遇到了某个节点的距离<0,就让它是0,0可以 ...
- Codeforces E. Alyona and a tree(二分树上差分)
题目描述: Alyona and a tree time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
随机推荐
- Android权限安全(13)4.3前后root原理不同
在JB MR2(4.3)之前 Apk内部可以通过Java的Runtime执行一个具有Root-setUID的可执行文件而 提升Effective UID来完成一些特权操作,典型的Root包中的su就是 ...
- ConcurrentDictionary<TKey, TValue>的AddOrUpdate方法
https://msdn.microsoft.com/zh-cn/library/ee378665(v=vs.110).aspx 此方法有一共有2个,现在只讨论其中一个 public TValue A ...
- 函数get_table_share
得到一个table_share 1)先从table_def_cache中查找, 如果有, 直接返回 2)如果没有找到, 为table_share分配内存,初始化,打开.frm文件,并将share ...
- uva12169 Disgruntled Judge
扩展欧几里得. 枚举a,根据x1,x3和递推式可得. (a+1)*b-k*mod=f[3]-a*a*b. 通过扩展欧几里得求出b. 带入原式进行计算. #include<cstdio> # ...
- Android-Universal-Image-Loader
基本以后都不用了,所以自己就不总结了 http://www.cnblogs.com/kissazi2/p/3886563.html http://www.cnblogs.com/kissazi2/p/ ...
- tomcat启动中提示 - consider increasing the maximum size of the cache
tomcat启动过程中提示: org.apache.catalina.webresources.Cache.getResource Unable to add the resource at xxx ...
- HDU 4607 Park Visit (DP最长链)
[题目]题意:N个城市形成一棵树,相邻城市之间的距离是1,问访问K个城市的最短路程是多少,共有M次询问(1 <= N, M <= 100000, 1 <= K <= N). [ ...
- UVA 10098 Generating Fast, Sorted Permutation
// 给你字符串 按字典序输出所有排列// 要是每个字母都不同 可以直接dfs ^_^// 用前面说的生成排列算法 也可以直接 stl next_permutation #include <io ...
- 【转】ios app 应用内购买配置完全指南
转自:http://blog.sina.com.cn/s/blog_4b55f6860100sbfb.html 第一印象觉得In-App Purchase(简称IAP)非常简单.Apple提供的大量文 ...
- 【转】移动web资源整理
目录(更新于20150311) meta基础知识 H5页面窗口自动调整到设备宽度,并禁止用户缩放页面 忽略将页面中的数字识别为电话号码 忽略Android平台中对邮箱地址的识别 当网站添加到主屏幕快速 ...