sgu134:Centroid

134. Centroid

time limit per test: 0.25 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
好像很水的一道题...
建一棵树,由于删去一个点,会把图分为它的祖宗结点一部分,k个儿子部分,记录f[i]表示以结点i为根的子树的结点数,那么祖宗结点个数为N-f[i],儿子即为f[i->son],
枚举每个结点并统计最大值中的最小值,即是答案,时间O(2(n-1)),递归求解f为O(n-1),枚举为O(n-1)
#include <cstdio>
#include <cstdlib>
#include <algorithm>
using namespace std;
const int MAXN = 16005, INF = 1<<30;
int N = 0;
struct Edge
{
  int node;
  Edge *nxt;	
}*e[MAXN] = {NULL}, *t[MAXN] = {NULL};

int used[MAXN] = {0}, top = 0; 

int f[MAXN] = {0};

void add(int x, int y)
{
  Edge *T;
  T = (Edge *)malloc(sizeof(Edge));
  T->node = y;
  T->nxt = NULL;
  if(e[x] == NULL)
    e[x] = t[x] = T;
  else 
  {
    t[x]->nxt = T;
	t[x] = t[x]->nxt;	
  }
}

void dfs(int cur)
{
  used[cur] = ++top;
  f[cur] = 1;
  for(Edge *i = e[cur]; i != NULL; i = i->nxt)	
    if(!used[i->node])
    {
      dfs(i->node);
	  f[cur] += f[i->node];
    }
}

int main()
{
  scanf("%d", &N);
  for(int i = 1; i < N; ++i)
  {
    int x, y;
	scanf("%d%d", &x, &y);
	add(x, y);add(y, x);	
  }
  
  dfs(1);
  
  int ans = INF, sum = 0;
  int ans2[MAXN] = {0};
  for(int i = 1; i <= N; ++i)
  {
    int maxsum = 0;
	maxsum = max(maxsum, N-f[i]);
	for(Edge *j = e[i]; j != NULL; j = j->nxt)
	  if(used[i] < used[j->node])	
	    maxsum = max(maxsum, f[j->node]);
	if(maxsum < ans)
	{
	  ans = maxsum;
	  sum = 1;
	  ans2[1] = i;	
	}
	else if(maxsum == ans)
	  ans2[++sum] = i;	
  }
  
  printf("%d %d\n", ans, sum);
  for(int i = 1; i <= sum; ++i)
  {
    printf("%d", ans2[i]);
    if(i < sum) printf(" ");
  }
  return 0;	
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值