CodeForces 682C Alyona and the Tree (树+dfs)
2016-07-21 10:50
489 查看
Alyona and the Tree
题目链接:
http://acm.hust.edu.cn/vjudge/contest/121333#problem/CDescription
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
Input9
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; }
相关文章推荐
- HTML基础知识
- Spring JdbcTemplate方法详解
- linux中安装FastDFS-v5.05+nginx-1.8.0
- spring 动态数据源不起作用
- 如何用JS判断两个数字的大小
- HDU 1028 母函数做法
- 单例模式
- 深度思考Java成员变量的初始化
- 图的割点/桥
- 程序员经常遇到的几个问题!
- Objective-C中属性及其特质@property、attribute
- 背景图布满整个页面
- Android 优雅的为RecyclerView添加HeaderView和FooterView
- hdu 5185 Equation(DP,思路)
- a 与 &a 的区别
- LeetCode之求组合数之和二CombinationSumII
- iOS—最全的真机测试教程
- 在iOS中使用字体文件.ttf(icon font,可缩放矢量图)
- 全国地区的省份、城市、区县 最新Sql脚本
- pat 1099. Build A Binary Search Tree (30)