-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTreeDiameter.cpp
More file actions
54 lines (45 loc) · 1.09 KB
/
TreeDiameter.cpp
File metadata and controls
54 lines (45 loc) · 1.09 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
//Tree Diameter - https://cses.fi/problemset/task/1131
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MOD = 1e9 + 7;
const ll INF = 1e18;
void solve() {
int n;
cin >> n;
vector<vector<int>> g(n);
for (int i = 0; i < n - 1; ++i) {
int u, v;
cin >> u >> v;
u--, v--;
g[u].push_back(v);
g[v].push_back(u);
}
auto dfs = [&](auto dfs, int u, int p) -> pair<int, int> {
pair<int, int> ans = {0, 0};
for (auto &v: g[u]) {
if (v == p) continue;
auto [depth, index] = dfs(dfs, v, u);
if (1 + depth > ans.first) {
ans = {1 + depth, index};
}
}
if ((int)g[u].size() == 1 && u != 0) {
ans.second = u;
}
return ans;
};
auto [val, index] = dfs(dfs, 0, -1);
auto [val2, index2] = dfs(dfs, index, -1);
cout << val2 << endl;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int T = 1;
// cin >> T;
while (T--) {
solve();
}
return 0;
}