1 second
256 megabytes
standard input
standard output
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.
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
5
The following image represents possible process of removing leaves from the tree:
题意 :给n个节点构成一棵树,并且1是根结点,如果u是在v的子树中的节点,那么当d(u,v)>ans[u]时此时v就是不开心的节点。然后让你删除节点,删除节点后使得剩下的
点都为开心点,让你求最少删除的点。
思路:dfs+dp;
从根节点深搜下去,然后dp[i]表示在这个节点上边并能到达这个点的最大值,那么当dp[i]>ans[i]时就表示当前这个节点,以及下面所有的子节点都要删除。
最后统计下没被遍历到的和被标记的。
1 #include<stdio.h>
2 #include<algorithm>
3 #include<iostream>
4 #include<string.h>
5 #include<stdlib.h>
6 #include<queue>
7 #include<math.h>
8 #include<vector>
9 using namespace std;
10 typedef long long LL;
11 LL ans[100006];
12 typedef struct pp
13 {
14 int id;
15 LL cost;
16 } ss;
17 void dfs(int n);
18 bool flag[100006];
19 LL dp[100006];
20 vector<ss>vec[100006];
21 int main(void)
22 {
23 int i,j,k;
24 while(scanf("%d",&k)!=EOF)
25 {
26 fill(dp,dp+100006,-1e16);
27 dp[1]=0;
28 for(i=1; i<=k; i++)
29 {
30 scanf("%lld",&ans[i]);
31 }
32 for(i=0; i<100006; i++)
33 vec[i].clear();
34 memset(flag,0,sizeof(flag));
35 for(i=0; i<k-1; i++)
36 {
37 int n;
38 LL m;
39 scanf("%d %lld",&n,&m);
40 ss ans;
41 ans.cost=m;
42 ans.id=i+2;
43 vec[n].push_back(ans);
44 }
45 dfs(1);
46 int remoe=0;
47 for(i=1; i<=k; i++)
48 {
49 if(flag[i])
50 {
51 remoe++;
52 }
53 if(dp[i]==-1e16)
54 remoe++;
55 }
56 printf("%d\n",remoe);
57 }
58 return 0;
59 }
60 void dfs(int n)
61 {
62 int i,j,k;
63 for(i=0; i<vec[n].size(); i++)
64 {
65 ss ak=vec[n][i];
66 int id=ak.id;
67 dp[id]=max(dp[id],dp[n]+ak.cost);
68 dp[id]=max(ak.cost,dp[id]);
69 if(dp[id]>ans[id])
70 {
71 flag[id]=true;
72 }
73 else
74 {
75 dfs(id);
76 }
77 }
78 }