题意:https://www.luogu.com.cn/problem/CF1406C
思路:首先需要知道树的重心的一些性质,可以看看这篇文章
https://zhuanlan.zhihu.com/p/357938161
那么这题就是找出重心,如果有两个就将他的一个子树移到另一个重心上。
/*keep on going and never give up*/
#include<cstdio>
#include<iostream>
#include<queue>
#include<algorithm>
using namespace std;
#define int long long
typedef pair<int, int> pii;
#define lowbit(x) x&(-x)
#define endl '\n'
#define wk is zqx ta die
vector<int> pl[100005];
int sz[100005], mss[100005];
vector<int> ctz;
int n;
void dfs(int p, int fa = 0) {
// cout << p << endl;
sz[p] = 1;
mss[p] = 0;
for (int i : pl[p]) {
if (i == fa) {
continue;
}
dfs(i, p);
sz[p] += sz[i];
mss[p] = max(mss[p], sz[i]);
}
mss[p] = max(mss[p], n - sz[p]);
if (mss[p] <= n / 2) {
ctz.push_back(p);
}
}
signed main() {
std::ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while (t--) {
ctz.clear();
cin >> n;
for (int i = 1; i <= n; i++) {
pl[i].clear();
}
for (int i = 1; i < n; i++) {
int u, v;
cin >> u >> v;
pl[u].push_back(v);
pl[v].push_back(u);
}
if (n & 1) {
cout << pl[1][0] << " " << 1 << endl;
cout << 1 << " " << pl[1][0] << endl;
} else {
dfs(1);
if (ctz.size() == 1) {
cout << pl[1][0] << " " << 1 << endl;
cout << 1 << " " << pl[1][0] << endl;
} else {
int a = ctz[0];
int b = ctz[1];
// cout << a << " " << b << endl;
int h = 0;
for (int i : pl[a]) {
if (i == b) {
continue;
}
h = i;
break;
}
cout << a << " " << h << endl;
cout << h << " " << b << endl;
}
}
}
return 0;
}