-
Notifications
You must be signed in to change notification settings - Fork 185
/
1131.cc
55 lines (52 loc) · 958 Bytes
/
1131.cc
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
// https://cses.fi/problemset/task/1131/
#include <iostream>
#include <vector>
#include <queue>
#include <tuple>
#include <unordered_set>
using namespace std;
typedef tuple<int, int> ii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef queue<ii> qii;
typedef unordered_set<int> si;
int main() {
int n;
cin >> n;
vvi g(n);
for (int i = 0; i < n - 1; i++) {
int a, b;
cin >> a >> b;
a--;
b--;
g[a].push_back(b);
g[b].push_back(a);
}
qii q;
si s;
q.push({0, 0});
s.insert(0);
int a, k;
while (!q.empty()) {
tie(a, k) = q.front();
q.pop();
for (auto z : g[a]) {
if (s.count(z)) continue;
s.insert(z);
q.push({z, k + 1});
}
}
s.clear();
q.push({a, 0});
s.insert(a);
while (!q.empty()) {
tie(a, k) = q.front();
q.pop();
for (auto z : g[a]) {
if (s.count(z)) continue;
s.insert(z);
q.push({z, k + 1});
}
}
cout << k << endl;
}