Codeforces 682C C. Alyona and the Tree (DFS)
来源:互联网 发布:淘宝退货卖家拖延时间 编辑:程序博客网 时间:2024/06/08 16:23
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 v sad 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?
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.
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.
988 22 83 14 95 91 98 53 113 247 -81 671 649 655 126 -803 8
5
The following image represents possible process of removing leaves from the tree:
#include<stdio.h>#include<string.h>#include<vector>using namespace std;const int maxn=1e5+5;vector<pair<int,int> >E[maxn];int a[maxn];int dfs(int x,int fa,int dis){ if(dis>a[x])return 0; int ans=1; for(int i=0;i<E[x].size();i++) { if(E[x][i].first==fa)continue; ans=ans+dfs(E[x][i].first,x,max(E[x][i].second+dis),0); } return ans;}int main(){ int t; scanf("%d",&t); for(int i=1;i<=t;i++) { scanf("%d",&a[i]); } for(int i=2;i<=t;i++) { int x,y; scanf("%d%d",&x,&y); E[i].push_back(make_pair(x,y)); E[x].push_back(make_pair(i,y)); } int ans=dfs(1,0,0); printf("%d\n",t-ans);}
- Codeforces 682C C. Alyona and the Tree (DFS)
- CodeForces 682C - Alyona and the Tree(dfs)
- codeforces 682C Alyona and the Tree(DFS)
- Codeforces 682C. Alyona and the Tree
- codeforces 682C. Alyona and the Tree
- codeforces 682C Alyona and the Tree
- Codeforces 682C Alyona and the Tree【Dfs+YY思维】好题
- CodeForces 682C Alyona and the Tree 搜索
- Codeforces 682C Alyona and the Tree DP
- Codeforces Round #358 (Div. 2) C. Alyona and the Tree (DFS)
- Codeforces Round #358 (Div. 2) -- C. Alyona and the Tree (树上的DFS)
- Codeforces Round #358 (Div. 2) C. Alyona and the Tree (二叉树+DFS)
- Codeforces Round #358 (Div. 2) C. Alyona and the Tree(DFS)
- C. Alyona and the Tree
- Codeforces Round #358 (Div. 2) C. Alyona and the Tree
- Codeforces Round #358 (Div. 2)C. Alyona and the Tree
- Codeforces Round #358 (Div. 2) C. Alyona and the Tree
- Codeforces Round #358 (Div. 2)——C. Alyona and the Tree(树的DFS+逆向思维)
- Service和IntentService
- c#控制台正则匹配
- Scrum到底怎么玩儿?
- 零碎知识笔记
- Ubuntu 16 安装php 5.5 的方法
- Codeforces 682C C. Alyona and the Tree (DFS)
- 线性表的删除及查找定位删除(完整程序)
- Method swizzling
- 日常小结-xml编程(CRUD)
- iOS CocoaPods 的安装流程
- Android 实现沉浸式状态栏效果(systembartint库)
- 第一次盲打测试
- GestureDetector_SimpleOnGestureListener
- 边做边学Python Flask Web开发(4)-- 使用Jinjia2模板(上)