GYM 100796C Minimax Tree

C. Minimax Tree
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Bob's new favourite toy is a rooted tree that consists of n vertices numbered from 1 to n. The number of the root vertex is 1. The tree has l leafs (the root is not considered to be a leaf). Each leaf of the tree has an integer written in it.

This birthday Bob received n - l stickers as a gift: k of them are labelled "min", and the other n - l - k are labelled "max". Bob has decided to place the stickers on the internal vertices of the tree, a single sticker on each internal vertex.

Once he has placed all the stickers on the tree, Bob would like to calculate a function f for each vertex v of the tree in the following fashion:

  • If v is a leaf, f(v) is equal to the integer that is written in v.
  • If v has a "min" sticker, f(v) is equal to the minimum value of f(u), where u is any child of v.
  • If v has a "max" sticker, f(v) is equal to the maximum value of f(u), where u is any child of v.

Bob isn't yet sure how to place his stickers on the tree, but he is interested in the value of f in the root vertex. Given the tree and the stickers, help Bob calculate the minimum and the maximum possible value of f(1)!

Input

The first line contains two space-separated integers n and k (2 ≤ n ≤ 1050 ≤ k ≤ n). The second line contains n - 1 space-separated integer numbers p2, p3, ..., pn (1 ≤ pi ≤ n). The number pi denotes the parent of the vertex numbered i. The third line contains nspace-separated integer numbers a1, a2, ..., an (0 ≤ ai ≤ 109). If the vertex i is a leaf, then ai is the number written in that vertex. Otherwise ai will be equal to 0.

It is guaranteed that the given graph will be a tree. It is guaranteed that k + l ≤ n.

Output

In a single line output two integers separated by a space — the minimum and the maximum possible value of f(1).

Sample test(s)
input
6 1
1 1 2 2 3
0 0 0 1 3 2
output
2 3
Note

tree is a connected graph that has no cycles. A rooted tree is a tree with one vertex being the root vertex. In a rooted tree, a vertex uis a child of v if and only if there is an edge between v and u, and u does not belong to the path that connects the root vertex with v. The vertex v then is called the parent of u. A vertex of a rooted tree is called a leaf if and only if it has no children. Otherwise the vertex is called an internal vertex.

题目链接

http://codeforces.com/gym/100796/problem/C

题目大意

给出一棵n个节点的树,有l个叶节点,每个叶节点都有一个value值,现有k个min标签,n-l-k个max标签,安排中间节点的标签,输出根节点的最大值和最小值。

解题思路

        就最小值来讲,要想让某一个节点的值最终传递到根节点,它的祖先节点中有两个或两个以上子节点的必须全部采用min标签,最大值亦然。

        所以dfs处理出每个节点的最大值和最小值,d<=k的节点中最大值的最小值就是根节点的最小值,d<=n-l-k的节点中最小值的最大值就是根节点的最大值。需要注意的就是一个节点只有一个子节点时,子节点的d值不+1。

AC代码

import java.util.*;
public class Main {
	static int n,k,l,ee,a,b;
	static int[] max=new int[100010];
	static int[] min=new int[100010];
	static int[] val=new int[100010];
	static int[] size=new int[100010];
	static int[] head=new int[100010];
	static int[] edge=new int[100010];
	static int[] next=new int[100010];
	static void add(int a,int b)
	{
		edge[ee]=b;next[ee]=head[a];
		head[a]=ee++;
	}
	static void dfs(int x,int d)
	{
		if(val[x]!=0) min[x]=max[x]=val[x];
		else { min[x]=2000000000;max[x]=0;}
		int v;
		for(int i=head[x];i!=-1;i=next[i])
		{
			v=edge[i];
			dfs(v,d+(size[v]==1?0:1));//坑点
			min[x]=Math.min(min[x],min[v]);
			max[x]=Math.max(max[x],max[v]);
		}
		if(d<=k) a=Math.min(a,max[x]);
		if(d<=n-l-k) b=Math.max(b,min[x]);
	}

	public static void main(String[] args) {
		Scanner in=new Scanner(System.in);
		n=in.nextInt();k=in.nextInt();
		Arrays.fill(head,-1);
		for(int i=2;i<=n;i++)
		{
			a=in.nextInt();
			add(a,i);size[a]++;
		}
		for(int i=1;i<=n;i++)
		{
			val[i]=in.nextInt();
			if(val[i]!=0) l++;
		}
		a=2000000000;b=0;
		dfs(1,0);
		System.out.println(a+" "+b);
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值