-
Notifications
You must be signed in to change notification settings - Fork 186
Expand file tree
/
Copy pathc.cc
More file actions
68 lines (62 loc) · 1.27 KB
/
c.cc
File metadata and controls
68 lines (62 loc) · 1.27 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
// https://codeforces.com/contest/1406/problem/C
#include <bits/stdc++.h>
using namespace std;
using vi = vector<int>;
using vvi = vector<vi>;
int n;
vvi g;
vi d;
void dfs(int u, int p) {
d[u] = 1;
for (int v : g[u])
if (v != p) {
dfs(v, u);
d[u] += d[v];
}
}
tuple<int, int> find_leave(int u, int p) {
if (g[u].size() == 1) return {u, p};
for (int v : g[u])
if (v != p) return find_leave(v, u);
assert(false);
return {0, 0};
}
void find(int u, int p) {
int mx = 0, k;
for (int v : g[u])
if (v != p && d[v] > mx) mx = d[v], k = v;
if (mx < n / 2) {
for (int i = 0; i < 2; i++) cout << u + 1 << ' ' << k + 1 << '\n';
return;
}
if (mx == n / 2) {
int p, q;
tie(p, q) = find_leave(k, u);
cout << q + 1 << ' ' << p + 1 << '\n';
cout << u + 1 << ' ' << p + 1 << '\n';
return;
}
find(k, u);
}
int main() {
cin.tie(0), ios::sync_with_stdio(0);
int t, u, v;
cin >> t;
while (t--) {
cin >> n;
g = vvi(n);
d = vi(n);
for (int i = 0; i < n - 1; i++) {
cin >> u >> v;
u--, v--;
g[u].push_back(v);
g[v].push_back(u);
}
if (n % 2) {
for (int i = 0; i < 2; i++) cout << u + 1 << ' ' << v + 1 << '\n';
continue;
}
dfs(0, -1);
find(0, -1);
}
}