You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

72 lines
1.6 KiB

2 years ago
#include <bits/stdc++.h>
#define N 100005
using namespace std;
int n, head[N], ver[2 * N], Next[2 * N], tot = 0;
void add(int x, int y) {
ver[++tot] = y, Next[tot] = head[x], head[x] = tot;
}
vector<int> c;
int mx[N], sz[N];
int mn = 0x3f3f3f3f;
void dfs(int x, int pre) {
for (int i = head[x]; i; i = Next[i]) {
int y = ver[i];
if (y == pre) continue;
dfs(y, x);
sz[x] += (sz[y] + 1);
mx[x] = max(mx[x], sz[y] + 1);
}
mx[x] = max(mx[x], n - 1 - sz[x]);
mn = min(mn, mx[x]);
}
void solve() {
cin >> n;
tot = 0;
mn = 0x3f3f3f3f;
c.clear();
for (int i = 0; i <= n; i++) {
head[i] = 0;
mx[i] = 0;
sz[i] = 0;
}
for (int i = 1; i < n; i++) {
int x, y;
cin >> x >> y;
add(x, y);
add(y, x);
}
dfs(1, 0);
for (int i = 1; i <= n; i++) {
if (mx[i] == mn) {
c.push_back(i);
}
}
if (c.size() == 1) {
for (int i = head[c[0]]; i; i = Next[i]) {
int y = ver[i];
cout << c[0] << " " << y << endl;
cout << c[0] << " " << y << endl;
return;
}
} else {
int tmp;
for (int i = head[c[0]]; i; i = Next[i]) {
int y = ver[i];
if (y == c[1]) continue;
cout << c[0] << " " << y << endl;
tmp = y;
break;
}
cout << c[1] << " " << tmp << endl;
return;
}
}
int main() {
int T;
cin >> T;
while (T--) {
solve();
}
return 0;
}