-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTreeDiameter.cpp
More file actions
63 lines (53 loc) · 925 Bytes
/
Copy pathTreeDiameter.cpp
File metadata and controls
63 lines (53 loc) · 925 Bytes
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
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define fi first
#define se second
int n;
const int Sz = 2e5;
vector <int> a[Sz + 1];
void Read()
{
cin >> n;
int u, v;
for(int i = 2; i <= n; i++)
{
cin >> u >> v;
a[u].push_back(v);
a[v].push_back(u);
}
}
int Len[Sz + 1];
void DFS(int u, int p)
{
for(int v : a[u])
{
if(v != p)
{
Len[v] = Len[u] + 1;
DFS(v, u);
}
}
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
Read();
DFS(1, 0);
int Max = 0;
int u = 0;
for(int i = 1; i <= n; i++)
{
if(Len[i] > Max)
{
Max = Len[i];
u = i;
}
}
for(int i = 1; i <= n; i++) Len[i] = 0;
DFS(u, 0);
Max = 0;
for(int i = 1; i <= n; i++) Max = max(Max, Len[i]);
cout << Max;
}