forked from varnitsingh/OJ-problems
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Link Cut Centroids.cpp
96 lines (72 loc) · 2.17 KB
/
Link Cut Centroids.cpp
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// Problem: C. Link Cut Centroids
// Contest: Codeforces - Codeforces Round #670 (Div. 2)
// URL: https://codeforces.com/contest/1406/problem/C
// Memory Limit: 512 MB
// Time Limit: 1000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)
#include <bits/stdc++.h>
#define F first
#define S second
#define PB push_back
#define MP make_pair
#define int long long int
#define vi vector<int>
#define vii vector<int, int>
#define vc vector<char>
#define vl vector<ll>
#define mod 1000000007
#define INF 1000000009
using namespace std;
vector<int> Centroid(const vector<vector<int>> &g) {
int n = g.size();
vector<int> centroid;
vector<int> sz(n);
function<void (int, int)> dfs = [&](int u, int prev) {
sz[u] = 1;
bool is_centroid = true;
for (auto v : g[u]) if (v != prev) {
dfs(v, u);
sz[u] += sz[v];
if (sz[v] > n / 2) is_centroid = false;
}
if (n - sz[u] > n / 2) is_centroid = false;
if (is_centroid) centroid.push_back(u);
};
dfs(0, -1);
return centroid;
}
signed main()
{
int t = 1; cin >> t;
for (int test = 1; test <= t; test++)
{
vector<vector<int> > adj;
int n; cin >> n;
adj.resize(n);
for (int i = 0; i < n - 1; i++)
{
int x, y; cin >> x >> y;
x--, y--;
adj[x].push_back(y);
adj[y].push_back(x);
}
auto centroid = Centroid(adj);
if(centroid.size() == 1) {
for(int node : adj[centroid[0]]) {
cout << node + 1 << " " << centroid[0] + 1 << "\n";
cout << node + 1<< " " << centroid[0] +1 << "\n";
break;
}
}
else {
for(int node : adj[centroid[0]]) {
if(node != centroid[1]) {
cout << centroid[0] + 1 << " " << node + 1 << "\n";
cout << centroid[1] + 1 << " "<< node + 1 << "\n";
break;
}
}
}
}
return 0;
}