#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
#include <vector>
#include <queue>
using namespace std;
int N;
vector<int> v[10005];
int book[10005]={0};
void dfs(int id)
{
book[id]=1;
for(int i=0;i<v[id].size();i++)
{
if(book[v[id][i]]==0)
dfs(v[id][i]);
}
return;
}
int deep[10005]={0};
int maxx;
void DFS(int id,int depth,int father)
{
if(v[id].size()==1&&v[id][0]==father)
{
book[id]=1;
if(depth>maxx)
maxx=depth;
return;
}
book[id]=1;
for(int i=0;i<v[id].size();i++)
{
if(book[v[id][i]]==0)
DFS(v[id][i],depth+1,id);
}
}
int main()
{
// freopen("in.txt","r",stdin);
scanf("%d",&N);
for(int i=0;i<N-1;i++)
{
int t1,t2;
scanf("%d %d",&t1,&t2);
v[t1].push_back(t2);
v[t2].push_back(t1);
}
int cou=0;
for(int i=1;i<=N;i++)
{
if(book[i]==0)
{
dfs(i);
cou++;
}
}
if(cou>1)
{
printf("Error: %d components",cou);
return 0;
}
for(int i=1;i<=N;i++)
{
memset(book,0,sizeof(book));
maxx=-1;
DFS(i,1,0);
deep[i]=maxx;
}
//for(int i=1;i<=N;i++)
// printf("%d: %d\n",i,deep[i]);
int m=-1;
for(int i=1;i<=N;i++)
{
if(deep[i]>m)
m=deep[i];
}
for(int i=1;i<=N;i++)
{
if(deep[i]==m)
printf("%d\n",i);
}
return 0;
}
1021. Deepest Root (25)
最新推荐文章于 2022-06-13 16:03:16 发布