-
Notifications
You must be signed in to change notification settings - Fork 19
/
diameterofatree.cpp
83 lines (79 loc) · 1.59 KB
/
diameterofatree.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
76
77
78
79
80
81
82
83
#include<bits/stdc++.h>
//#include<boost/multiprecision/cpp_int.hpp>
//using boost::multiprecision::cpp_int; // use cpp_int as data type for large numbers
using namespace std;
#define ll long long
#define pb push_back
#define fast() ios_base::sync_with_stdio(false); cin.tie(NULL);
#define mod 1000000007
#define mp make_pair
#define ff first
#define ss second
#define vi vector<ll>
#define vll vector<long long>
#define log2(X) (63ll - __builtin_clzll(X))
#define test ll t; cin>>t; while(t--)
#define modInv(n) po(n,mod-2)%mod
#define ncr(n,r) (((fact[n]*modInv(fact[r]))%mod)*modInv(fact[n-r]))%mod
#define fo(i,n) for(i=1;i<n;i++)
// use endl for interactive problems
ll po(ll a, ll b)
{
ll res=1;
while(b)
{
if(b&1)
{
res=(res*a)%mod;
}
a=(a*a)%mod;
b=b/2;
}
return res%mod;
}
ll n;
vll adj[2*100005];
ll visited[2*100005];
ll res;
ll coun,max_count;
ll dfs(ll i)
{
coun++;
if(coun>=max_count)
{
max_count=coun;
res=i;
}
visited[i]=1;
for(auto v: adj[i])
{
if(!visited[v])
{
dfs(v);
}
}
coun--;
return max_count;
}
int main()
{
fast()
//test
{
cin>>n;
ll u,v;
int i;
fo(i,n)
{
cin>>u>>v;
adj[u].pb(v);
adj[v].pb(u);
}
dfs(1ll);
memset(visited,0ll,sizeof(visited));
coun=0,max_count=0;
ll ans=dfs(res)-1;
cout<<ans<<"\n";
}
return 0;
}