Method: Starting from the root node wide search, if you encounter the point should be deleted, and then a wide search to delete its sub-tree and mark, and then count the number of marks is the answer, the so-called technique is to start from the root node search, if encountered a node distance <0, let it is 0,0 can eliminate negative effects, Let the latter point re-participate in the normal deletion operation, the correctness of this method is not difficult to prove that you can draw one yourself. And there are more convenient ways to record the points that are not deleted, and then n-they can.
The code is as follows:
#include <iostream>#include<cstdio>#include<cstring>#include<vector>#include<queue>using namespacestd;#defineN 100010#defineINF 1e9structnode{intTo,val;}; Vector<Node>Vt[n];intA[n],n,vis[n],del[n],fa[n],mark[n];queue<int>Que;queue<int>Q1;voidDelete (intu) { intLen,v,now; while(!q1.empty ()) Q1.pop (); Q1.push (U); Del[u]=1; while(!Q1.empty ()) { Now=Q1.front (); Q1.pop (); Len=vt[now].size (); for(inti =0; i < Len; i++) {v=vt[now][i].to; if(!del[v] && fa[u]! =v) {Del[v]=1; Q1.push (v); } } }}voidBFsints) {Que.push (s); Vis[s]=0; intNow,nxt,len,dis; while(!Que.empty ()) { Now=Que.front (); Que.pop (); Len=vt[now].size (); for(inti =0; i < Len; i++) {NXT=vt[now][i].to; Dis=Vt[now][i].val; FA[NXT]=Now ; if(VIS[NXT] = =INF) {VIS[NXT]= Vis[now] +dis; if(VIS[NXT] <0) VIS[NXT] =0; if(VIS[NXT] >A[NXT]) {Delete (NXT); } ElseQue.push (NXT); } } }}intMain () {memset (Del,0,sizeof(del)); scanf ("%d",&N); for(inti =1; I <= N; i++) {scanf ("%d",&A[i]); Vt[i].clear (); Fa[i]=i; Vis[i]=INF; } intB,wei; Node L,r; for(inti =2; I <= N; i++) {scanf ("%d%d",&b,&Wei); R.to= b; R.val =Wei; Vt[i].push_back (R); L.to= i; L.val =Wei; Vt[b].push_back (L); } while(!que.empty ()) Que.pop (); BFS (1); intAns =0; for(inti =1; I <= N; i++) { if(Del[i]) {ans++; }} printf ("%d\n", ans); return 0;}
Codeforces 682C Alyona and the Tree (wide search + tips)