-
Notifications
You must be signed in to change notification settings - Fork 0
/
pat 1021.cpp
75 lines (71 loc) · 1.75 KB
/
pat 1021.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <cstdio>
#include <cstdlib>
#include <list>
#include <cstring>
using namespace std;
int N;
int visit[10001]={0};
list<int> G[10001];
int dfs(int i)
{
int max = 0 , temp;
visit[i] = 1;
list<int>::const_iterator p;
for( p = G[i].begin() ; p != G[i].end(); p++)
{
if( !visit[*p] )
{
temp = dfs(*p);
if( temp > max)
max = temp;
}
}
return max + 1;
}
int main()
{
int i,a,b,components=0;
scanf("%d",&N);
for( i= 0 ; i < N-1 ; i++)
{
scanf("%d%d",&a,&b);
G[a].push_back(b);
G[b].push_back(a);
}
for( i = 1 ; i <= N ; i++)
{
if( !visit[i] )
{
dfs(i);
components++;
}
}
if(components != 1)
{
printf("Error: %d components",components);
return 0;
}
list<int> maxnodes;
int max = 0;
for( i =1 ; i <= N ; i++)
{
memset(visit,0,sizeof(visit));
int temp = dfs(i);
if( temp > max)
{
maxnodes.clear();
maxnodes.push_back(i);
max = temp;
}
else if( temp == max)
{
maxnodes.push_back(i);
}
}
maxnodes.sort();
list<int>::const_iterator pos;
for( pos = maxnodes.begin() ; pos != maxnodes.end() ; pos++)
{
printf("%d\n",*pos);
}
}