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

poj1655 树的重心 树形dp

时间:2016-02-28 12:29:59      阅读:117      评论:0      收藏:0      [点我收藏+]

标签:

 处理处每个节点的孩子有几个,和树的大小就好了。

#include<cstdio>
#include<queue>
#include<cstring>
#include<iostream>
#include<algorithm>
#define INF 99999999
using namespace std;
const int MAXN = 20010;
struct node
{
        int to;
        int v;
        int next;
}edge[MAXN*2];
int p,len;
int num[MAXN];
int vis[MAXN],pre[MAXN],ind,n;
int siz[MAXN];//the size of the tree
int h[MAXN];//the maxnum of subtree
int way;
void add(int x,int y)
{
        edge[ind].to = y;
        edge[ind].next = pre[x];
        pre[x] = ind ++;
}
void dfs1(int rt)
{
        int i;
        vis[rt] = 1;
        siz[rt] = 1;
        for(i=pre[rt]; i!=-1; i=edge[i].next){
                int t = edge[i].to;
                if(!vis[t]){
                        dfs1(t);
                        siz[rt] += siz[t];
                        h[rt] = max(h[rt],siz[t]);
                }
        }
}
int main()
{
        #ifndef ONLINE_JUDGE
        freopen("data.txt","r",stdin);
        #endif
        int i,j,t;
        scanf("%d",&t);
        while(t--)
        {
                scanf("%d",&n);
                ind = 1;
                memset(pre,-1,sizeof(pre));
                for(i=1; i<n; i++){
                        int x,y;
                        scanf("%d%d",&x,&y);
                        add(x,y);
                        add(y,x);
                }
                memset(vis,0,sizeof(vis));
                memset(siz,0,sizeof(siz));
                memset(h,0,sizeof(h));
                dfs1(1);
                int ans = INF;
                int f;
                for(i=1; i<=n; i++){
                    int temp = max(h[i],n-siz[i]);
                    if(ans > temp){
                        ans = temp;
                        f = i;
                    }
                }
                //cout<<siz[1]<<endl;
                cout<<f<<" "<<ans<<endl;
        }
}

 

poj1655 树的重心 树形dp

标签:

原文地址:http://www.cnblogs.com/sweat123/p/5224312.html

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