码迷,mamicode.com
首页 > 其他好文 > 详细

poj 1694 An Old Stone Game 树形dp

时间:2015-07-02 12:09:15      阅读:148      评论:0      收藏:0      [点我收藏+]

标签:poj   算法   

//poj 1694
//sep9
#include <iostream>
#include <algorithm>
using namespace std;
const int maxN=256;
int n;
int tree[maxN][maxN];
int ans[maxN];
int cmp(int a,int b)
{
	return a>b;
}
int dfs(int u)
{
	int tmp[maxN],t=0;
	if(tree[u][0]==0)
		return ans[u]=1;
	for(int i=1;i<=tree[u][0];++i){
		dfs(tree[u][i]);
		tmp[t++]=ans[tree[u][i]];
	}
	sort(tmp,tmp+t,cmp);
	int maxx=-1;
	for(int i=0;i<t;++i)
		maxx=max(maxx,tmp[i]+i);
	return ans[u]=maxx;	
}

int main()
{
	int cases;
	scanf("%d",&cases);
	while(cases--){
		scanf("%d",&n);
		for(int i=1;i<=n;++i)
			tree[i][0]=0;
		for(int i=0;i<n;++i){
			int u,x;
			scanf("%d%d",&u,&x);
			while(x--){
				int v;
				scanf("%d",&v);
				tree[u][++tree[u][0]]=v;
			}
		}
		printf("%d\n",dfs(1));
	}	
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

poj 1694 An Old Stone Game 树形dp

标签:poj   算法   

原文地址:http://blog.csdn.net/sepnine/article/details/46723663

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!