1021
#include<iOStream>
#include<cstring>
#include<vector>
using namespace std;
//bool Map[10010][10010];
vector<int> Map[10010];
bool visited[10010];
short ans[10010];
int n;
void DFS(int v,int len);
int Search();
void Find();
int MaxLen;
int main()
{
cin >> n;
for (int i = 0; i < n - 1; i++)
{
int a, b;
cin >> a >> b;
Map[a].push_back(b);
Map[b].push_back(a);
}
int k = Search();
if (k > 1)
{
cout << "ERROR: " << k << " components" << endl;
}
else
{
Find();
}
system("pause");
return 0;
}
void Find()
{
int maxans = 0;
for (int i = 1; i <= n; i++)
{
MaxLen = 0;
memset(visited, 0, sizeof(visited));
DFS(i,0);
ans[i] = MaxLen;
if (MaxLen > maxans)
maxans = MaxLen;
}
for (int i = 1; i <= n; i++)
{
if (ans[i] == maxans)
cout << i << endl;
}
}
int Search()
{
memset(visited, 0, sizeof(visited));
int k = 0;
for (int i = 1; i <= n; i++)
{
if (visited[i] == 0)
{
k++;
DFS(i,0);
}
}
return k;
}
void DFS(int v,int len)
{
visited[v] = 1;
MaxLen = (len > MaxLen) ? len : MaxLen;
for (int i = 0; i < Map[v].size(); i++)
{
if (!visited[Map[v][i]])
DFS(Map[v][i], len + 1);
}
}
相关阅读