forked from knakul853/spoj
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PT07.cpp
56 lines (44 loc) · 992 Bytes
/
PT07.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
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
vector<int>g[10007];
int mark[10006];
int dist[10008];
int ans;
int bfs(int u)
{
memset(dist,0,sizeof(dist));
memset(mark,0,sizeof(mark));
queue<int>qu;
qu.push(u);
while(!qu.empty()){
int u = qu.front();
qu.pop();
for(auto i:g[u])
{
if(mark[i])continue;
else{
qu.push(i);
dist[i]=dist[u]+1;
mark[i]=1;
}
}
}
int co = max_element(dist,dist+10000)-dist;
ans=dist[co];
return co;
}
int main()
{
int n;
cin>>n;
for(int i=0;i<n-1;i++)
{
int x,y;
cin>>x>>y;
g[x].pb(y);
g[y].pb(x);
}
bfs(bfs(1));
cout<<ans<<endl;
}