SGU 134(树的重心)

134. Centroid

time limit per test: 0.5 sec. 
memory limit per test: 4096 KB

You are given an undirected connected graph, with N vertices and N-1 edges (a tree). You must find the centroid(s) of the tree. 
In order to define the centroid, some integer value will be assosciated to every vertex. Let's consider the vertex k. If we remove the vertex k from the tree (along with its adjacent edges), the remaining graph will have only N-1 vertices and may be composed of more than one connected components. Each of these components is (obviously) a tree. The value associated to vertex k is the largest number of vertices contained by some connected component in the remaining graph, after the removal of vertex k. All the vertices for which the associated value is minimum are considered centroids.

 

Input

The first line of the input contains the integer number N (1<=N<=16 000). The next N-1 lines will contain two integers, a and b, separated by blanks, meaning that there exists an edge between vertex a and vertex b.

 

Output

You should print two lines. The first line should contain the minimum value associated to the centroid(s) and the number of centroids. The second line should contain the list of vertices which are centroids, sorted in ascending order.

Sample Input

 

7
1 2
2 3
2 4
1 5
5 6
6 7

Sample Output

 

3 1
1

 

Author : Mugurel Ionut Andreica
Resource : SSU::Online Contester Fall Contest #2
Date : Fall 2002

 

 

 

 

给出点的个数和几条边,求出树的重心centre及与重心相连的那个连通块中最多顶点的个数V,输出数据分为两行,一是V ,centre,第二行是重心的数目centre_num;

 

/**********************
* author:crazy_石头
* Pro:SGU 134(求树的重心)
* algorithm:dfs
* Time:31ms
* Judge Status:Accepted
***********************/
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <queue>

using namespace std;

#define rep(i,h,n) for(int i=(h);i<=(n);i++)
#define ms(a,b) memset((a),(b),sizeof(a))
#define eps 1e-6
#define INF 1<<29
#define LL __int64
const int maxn=20000+5;
const int maxm=200+10;

int dp[maxn],num[maxn],vis[maxn];
struct Edge
{
    int to,next;
}edge[maxn<<1];

int head[maxn];
int cnt,n,m;
int centre[maxn];

inline void addedge(int u,int v)
{
    edge[cnt].to=v;
    edge[cnt].next=head[u];
    head[u]=cnt++;
}

inline void add(int u,int v)
{
    addedge(u,v);
    addedge(v,u);
}

inline void init()
{
    cnt=0,ms(head,-1);
}

inline void dfs(int u,int father)
{
    dp[u]=0,num[u]=1,vis[u]=1;
    for(int i=head[u];~i;i=edge[i].next)
    {
        int v=edge[i].to;
        if(v==father)continue;
        if(!vis[v])
        {
            dfs(v,u);
            dp[u]=max(dp[u],num[v]);
            num[u]+=num[v];
        }
    }
    dp[u]=max(dp[u],n-num[u]);
}

int main()
{
    int test,tot;
    cin>>n;
    init(),ms(vis,0);
    rep(i,1,n-1)
    {
        int u,v;
        cin>>u>>v;
        add(u,v);
    }
    dfs(1,-1);
    int centre_num=1;
    int index=1,ret=dp[1];
    rep(i,2,n)
    {
        if(ret>dp[i])
        {
            ret=dp[i];
            centre_num=1;//初始化重心只有1个;
        }
        else if(dp[i]==ret)
        centre_num++;
    }
    cout<<ret<<" "<<centre_num<<endl;
    rep(i,1,n)
    if(dp[i]==ret)
    cout<<i<<" ";
    return 0;
}


 

 

 

 

你可能感兴趣的:(SGU 134(树的重心))