-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1131 - Tree Diameter.cpp
More file actions
41 lines (35 loc) · 886 Bytes
/
1131 - Tree Diameter.cpp
File metadata and controls
41 lines (35 loc) · 886 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
#include <bits/stdc++.h>
using namespace std;
vector<vector<int>> a;
void find_furthest_vertex(int v, pair<int, int> &f, vector<bool> &marked,
int d = 0) {
if (marked[v]) return;
marked[v] = true;
if (d > f.second) {
f.second = d;
f.first = v;
}
for (const auto &item : a[v]) {
find_furthest_vertex(item, f, marked, d + 1);
}
marked[v] = false;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int n;
cin >> n;
a.resize(n + 1);
for (int i = 0; i < n - 1; ++i) {
int x, y;
cin >> x >> y;
a[y].push_back(x);
a[x].push_back(y);
}
pair<int, int> f{1, 0};
find_furthest_vertex(1, f, *(new vector<bool>(n + 1, false)));
find_furthest_vertex(f.first, f, *(new vector<bool>(n + 1, false)));
cout << f.second << "\n";
return 0;
}