-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTree Diameter.cpp
91 lines (90 loc) · 1.87 KB
/
Tree Diameter.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
84
85
86
87
88
89
90
91
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define AB_BHI_NI_DEGI \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL);
#define int long long
#define pb push_back
#define N 100009
#define inf 1e18
const double PI = 3.141592653589793238462643383279;
int mod = 1e9 + 7;
//int mod = 998244353;
#define P pair<int, int>
#define F first
#define S second
#define all(v) v.begin(), v.end()
#define vi vector<int>
#define ld long double
#define ordered_set tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>
vector<int> G[2 * N];
int dis[2 * N];
void bfs(int src, int n)
{
for (int i = 1; i <= n; i++)
dis[i] = 0;
int vis[n + 1] = {0};
vis[src] = 1;
queue<int> q;
q.push(src);
while (q.size())
{
int sz = q.size();
while (sz--)
{
int u = q.front();
q.pop();
for (int v : G[u])
{
if (!vis[v])
{
vis[v] = 1;
dis[v] = dis[u] + 1;
q.push(v);
}
}
}
}
}
void solve()
{
int n;
cin >> n;
for (int i = 2; i <= n; i++)
{
int u, v;
cin >> u >> v;
G[u].pb(v);
G[v].pb(u);
}
bfs(1, n);
int mx1 = 1;
for (int i = 1; i <= n; i++)
{
if (dis[i] > dis[mx1])
mx1 = i;
}
bfs(mx1, n);
int ans = 0;
for (int i = 1; i <= n; i++)
ans = max(ans, dis[i]);
cout << ans << "\n";
}
int32_t main()
{
AB_BHI_NI_DEGI
int Test = 1;
//cin >> Test;
//init();
int itr = 1;
while (Test--)
{
//cout << "Case #" << itr++ << ": ";
solve();
}
return 0;
}