题意
给出一棵树,dist(u,v)>au的点为sad点,dis(u,v)为从u到子结点v上的权值之和,au为结点u上的权值。
计算删除多少个叶子结点,使得该树不存在sad点。
dfs 从根结点开始计算,假设dist>au 那么 以该结点作为父节点的子树都不符合条件。
假设dist<0 那么 dist=0,假设在结点k因为对于k的父节点来说 k的祖先到k结点的权值和肯定会<=任何子结点到父节点k的权值和,因此这边dist取0即可。
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.Scanner;
public class MyC682 {
static int[] vertex;
static int[] status;
static List<Integer>[] children;
static int[] edge;
static int treecount(int node){
int treesize=0;
Queue<Integer> queue=new LinkedList<Integer>();
queue.add(node);
while(!queue.isEmpty()){
int temp=queue.remove();
treesize++;
queue.addAll(children[temp]);
}
return treesize;
}
static int dfs(int dist,int node){
int count=0;
if(dist>vertex[node])
{
count=treecount(node);
}
else {
for(int child:children[node]){
count+=dfs(Math.max(dist+edge[child], 0),child);
}
}
return count;
}
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
int n=in.nextInt();
vertex=new int[n];
edge=new int[n];
children=new List[n];
for(int i=0;i<n;i++) {vertex[i]=in.nextInt(); children[i]=new ArrayList<Integer>();}
for(int i=1;i<n;i++){
int q,c;
q=in.nextInt()-1;
c=in.nextInt();
children[q].add(i);
edge[i]=c;
}
System.out.println(dfs(0,0));
}
}