SGU134 Centroid 树形DP

简单树形DP,求树的质心:

最大的子树节点数最小的节点


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


#include<cstdio>
#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
#include<vector>

using namespace std;

#define MAXN 17000

struct edges
{
    int to,next;
}edge[49999];

int head[MAXN],en,n;
int total[MAXN],dp[MAXN];
vector<int> a;

void add(int u,int v)
{
    edge[en].to=v;
    edge[en].next=head[u];
    head[u]=en++;

    edge[en].to=u;
    edge[en].next=head[v];
    head[v]=en++;
}

void dfs(int u)
{
    dp[u]=0;total[u]=1;
    for(int i=head[u];i!=-1;i=edge[i].next)
    {
        int v=edge[i].to;
        if(total[v]==-1)
            dfs(v);
        else
            continue;
        dp[u]=max(dp[u],total[v]);
        total[u]+=total[v];
    }
}

int main()
{
    cin>>n;
    int u,v;
    a.clear();
    memset(head,-1,sizeof(head));en=0;
    for(int i=1;i<n;i++)
    {
        cin>>u>>v;
        add(u,v);
    }
    memset(dp,0,sizeof(dp));memset(total,-1,sizeof(total));
    dfs(1);
    int mins=0x3f3f3f3;
    for(int i=1;i<=n;i++)
    {
        int tmp=max(dp[i],n-total[i]);

        if(tmp<mins)
        {
            a.clear();
            a.push_back(i);
            mins=tmp;
        }
        else if(tmp==mins)
            a.push_back(i);
    }
    cout<<mins<<" "<<a.size()<<endl;
    sort(a.begin(),a.end());
    cout<<a[0];
    for(int i=1;i<a.size();i++)
        cout<<" "<<a[i];
    cout<<endl;
    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值