-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathL_Tree_Diameter.cpp
More file actions
75 lines (68 loc) · 1.35 KB
/
L_Tree_Diameter.cpp
File metadata and controls
75 lines (68 loc) · 1.35 KB
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
#include <iostream>
#include <vector>
#include <queue>
#include <stack>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <string>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <cassert>
#include <limits>
#include <numeric>
#include <climits>
#define int long long
using namespace std;
int n;
vector<vector<int>> adj;
vector<int> height;
vector<bool> visited;
int ans = 0;
void dfs(int node) {
visited[node] = true;
int mx1 = 0, mx2 = 0;
for (int nb : adj[node]) {
if (!visited[nb]) {
dfs(nb);
int h = height[nb] + 1;
if (h > mx1) {
mx2 = mx1;
mx1 = h;
} else if (h > mx2) {
mx2 = h;
}
}
}
ans = max(ans, mx1 + mx2);
height[node] = mx1;
}
void solve() {
cin >> n;
adj.assign(n+1 , vector<int>());
height.assign(n+1 , 1);
visited.assign(n+1 , false);
for(int i=0 ; i<n-1 ; i++){
int a , b;
cin >> a >> b;
adj[a].push_back(b);
adj[b].push_back(a);
}
dfs(1);
cout << ans << endl;
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
int _t=1;
// cin >> _t;
while(_t--){
solve();
}
return 0;
}