codeforces 682 C

题意

给出一棵树,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));
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值