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.

50 lines
1.3 KiB

2 years ago
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 100;
typedef long long LL;
vector<LL> g[maxn];
LL siz[maxn], son[maxn];
LL r1, r2, n;
void dfs(LL u, LL fa) {
siz[u] = 1;
son[u] = 0;
for (LL i = 0; i < g[u].size(); i++) {
LL v = g[u][i];
if (v == fa) continue;
dfs(v, u);
siz[u] += siz[v];
son[u] = max(son[u], siz[v]);
}
son[u] = max(son[u], n - siz[u]);
if ((son[u] << 1) <= n) r2 = r1, r1 = u;
}
int main() {
LL t;
cin >> t;
while (t--) {
cin >> n;
for (LL i = 0; i <= n + 10; i++) g[i].clear(), siz[i] = 0, son[i] = 0;
for (LL i = 1; i < n; i++) {
LL x, y;
cin >> x >> y;
g[x].push_back(y);
g[y].push_back(x);
}
r1 = r2 = 0;
dfs(1, 0);
if (!r2) {
LL r3 = g[r1][0];
cout << r1 << " " << r3 << endl;
cout << r1 << " " << r3 << endl;
} else {
LL r3 = r1;
for (LL i = 0; i < g[r2].size(); i++) {
r3 = g[r2][i];
if (r3 != r1) break;
}
cout << r3 << " " << r2 << endl;
cout << r3 << " " << r1 << endl;
}
}
return 0;
}