forked from mohitsh/SPOJ
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpt07z.cpp
75 lines (75 loc) · 1.03 KB
/
pt07z.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
// 2008-07-29
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <list>
using namespace std;
int main()
{
int N,i,u,v;
scanf("%d",&N);
list<int> L[10000];
for (i=0; i<N-1; i++)
{
scanf("%d %d",&u,&v); u--,v--;
L[u].push_back(v);
L[v].push_back(u);
}
int size=1;
int p[20000];
int n[20000];
int d[20000];
int maxdist=0;
int node;
p[0]=-1;
n[0]=0;
d[0]=0;
while (size)
{
size--;
int _n=n[size];
int _d=d[size];
int _p=p[size];
if (maxdist<_d)
{
maxdist=_d;
node=_n;
}
list<int>::iterator I;
for (I=L[_n].begin();I!=L[_n].end();I++)
if (*I!=_p)
{
p[size]=_n;
n[size]=*I;
d[size]=_d+1;
size++;
}
}
size=1;
maxdist=0;
p[0]=-1;
n[0]=node;
d[0]=0;
while (size)
{
size--;
int _n=n[size];
int _d=d[size];
int _p=p[size];
if (maxdist<_d)
{
maxdist=_d;
node=_n;
}
list<int>::iterator I;
for (I=L[_n].begin();I!=L[_n].end();I++)
if (*I!=_p)
{
p[size]=_n;
n[size]=*I;
d[size]=_d+1;
size++;
}
}
printf("%d\n",maxdist);
return 0;
}