版权声明:// Copyright © 2018年 Coding18. All rights reserved. https://blog.csdn.net/Coding18/article/details/86217021
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
const int maxn = 10010;
vector <int> G[maxn];
int father[maxn];
vector<int> temp,root;
int n;
int maxh = -1;
int findfather(int n)
{
int x = n;
while(x != father[x])
{
x = father[x];
}
int a = n;
while(father[a] != a)
{
int z = a;
a = father[a];
father[z] = x;
}
return x;
}
void Union(int a,int b)
{
int faa = findfather(a),fbb = findfather(b);
if(faa != fbb) father[faa] = fbb;
}
void dfs(int u,int h,int pre)
{
if(h > maxh)
{
temp.clear();
maxh = h;
temp.push_back(u);
}
else if(h == maxh)
{
temp.push_back(u);
}
for(int i = 0; i < G[u].size(); i++)
{
if(G[u][i] != pre)
{
// printf("%d",G[u][i]);
dfs(G[u][i],h+1,u);
}
}
}
int main()
{
scanf("%d",&n);
for(int i = 1; i <= n; i++)
{
father[i] = i;
}
int u,v;
for(int i = 1; i < n; i++)
{
scanf("%d %d",&u,&v);
G[u].push_back(v);
G[v].push_back(u);
Union(u,v);
}
int sum = 0;
for(int i = 1; i <= n; i++)
{
if(findfather(i) == i)
{
sum++;
}
}
if(sum > 1)
{
printf("Error: %d components\n",sum);
return 0;
}
dfs(1,1,-1);
root = temp;
dfs(root[0],1,-1);
for(int i = 0; i < temp.size(); i++)
{
root.push_back(temp[i]);
}
int len = root.size();
sort(root.begin(),root.end());
for(int i = 0; i < len; i++)
{
if(i >= 1 && root[i] == root[i-1]) continue;
printf("%d\n",root[i]);
}
return 0;
}