zoukankan      html  css  js  c++  java
  • poj 1694 An Old Stone Game 树形dp

    //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
    ",dfs(1));
    	}	
    }


  • 相关阅读:
    用面向对象做练习题
    面向对象
    php数组
    PHP函数
    php语句函数
    mySQL函数
    mySQL相关知识
    mySQL建表及练习题(下)
    mySQL建表及练习题(上)
    HTML5 canvas画布标签
  • 原文地址:https://www.cnblogs.com/gccbuaa/p/7212091.html
Copyright © 2011-2022 走看看