345 lines
8.6 KiB
C++
345 lines
8.6 KiB
C++
/* Problem URL: https://codeforces.com/contest/2107/problem/D */
|
|
|
|
#include <bits/stdc++.h>
|
|
|
|
using namespace std;
|
|
|
|
#define V vector
|
|
|
|
#define rmin(a, b) a = min(a, b)
|
|
#define rmax(a, b) a = max(a, b)
|
|
|
|
#define rep(i, lim) for (int i = 0; i < (lim); i++)
|
|
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
|
|
|
|
#define repv(i, v) for (auto &i : (v))
|
|
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
|
#define sortv(v) sort(v.begin(), v.end())
|
|
#define all(v) (v).begin(), (v).end()
|
|
|
|
using vi = vector<int>;
|
|
using vvi = vector<vi>;
|
|
using vvvi = vector<vvi>;
|
|
using vvvvi = vector<vvvi>;
|
|
|
|
using ll = long long;
|
|
|
|
using vl = vector<ll>;
|
|
using vvl = vector<vl>;
|
|
using vvvl = vector<vvl>;
|
|
using vvvvl = vector<vvvl>;
|
|
|
|
template<class v>
|
|
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
|
os << vec[0];
|
|
for (size_t i = 1; i < vec.size(); i++) {
|
|
os << ' ' << vec[i];
|
|
}
|
|
os << '\n';
|
|
return os;
|
|
}
|
|
|
|
template<class v>
|
|
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
|
for (auto &i : vec) {
|
|
is >> i;
|
|
}
|
|
return is;
|
|
}
|
|
|
|
template<class v>
|
|
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
|
for (auto &i : vec) {
|
|
os << i[0];
|
|
for (size_t j = 1; j < i.size(); j++) {
|
|
os << ' ' << i[j];
|
|
}
|
|
os << '\n';
|
|
}
|
|
return os;
|
|
}
|
|
|
|
template<class v>
|
|
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
|
for (auto &i : vec) {
|
|
for (auto &j : i) {
|
|
is >> j;
|
|
}
|
|
}
|
|
return is;
|
|
}
|
|
|
|
#define MAX_N 150000
|
|
|
|
vvi graph;
|
|
int inf = INT32_MAX >> 1;
|
|
int n;
|
|
int parent[MAX_N];
|
|
int vis[MAX_N];
|
|
int dis[MAX_N];
|
|
queue<int> used;
|
|
|
|
V<tuple<int, int, int>> ans;
|
|
|
|
int main()
|
|
{
|
|
ios::sync_with_stdio(false);
|
|
cin.tie(nullptr);
|
|
|
|
fill(dis, dis + MAX_N, inf);
|
|
|
|
int t;
|
|
cin >> t;
|
|
while (t--) {
|
|
cin >> n;
|
|
graph.assign(n, vi());
|
|
fill(vis, vis + n, inf);
|
|
|
|
rep(i, n - 1) {
|
|
int a, b;
|
|
cin >> a >> b;
|
|
a--, b--;
|
|
|
|
graph[a].push_back(b);
|
|
graph[b].push_back(a);
|
|
}
|
|
|
|
ans.clear();
|
|
|
|
function<void(int, int)> solve = [&solve](int i, int t){
|
|
int diam;
|
|
|
|
auto getcenter = [&](int i) {
|
|
int inf = INT32_MAX;
|
|
|
|
auto bfs = [&](int i) {
|
|
dis[i] = 0;
|
|
used.push(i);
|
|
|
|
int ans = i;
|
|
|
|
queue<int> q;
|
|
q.push(i);
|
|
|
|
while (!q.empty()) {
|
|
int i = q.front();
|
|
q.pop();
|
|
|
|
for (auto j : graph[i]) {
|
|
if (vis[j] >= t && dis[j] > dis[i] + 1) {
|
|
used.push(j);
|
|
dis[j] = dis[i] + 1;
|
|
ans = j;
|
|
parent[j] = i;
|
|
q.push(j);
|
|
}
|
|
}
|
|
}
|
|
|
|
return ans;
|
|
};
|
|
|
|
int now = bfs(i);
|
|
while (!used.empty()) {
|
|
dis[used.front()] = inf;
|
|
used.pop();
|
|
}
|
|
now = bfs(now);
|
|
diam = dis[now] + 1;
|
|
|
|
while (!used.empty()) {
|
|
dis[used.front()] = inf;
|
|
used.pop();
|
|
}
|
|
|
|
rep(i, diam / 2) {
|
|
now = parent[now];
|
|
}
|
|
|
|
return now;
|
|
};
|
|
|
|
int center = getcenter(i);
|
|
|
|
if (diam == 1) {
|
|
ans.emplace_back(diam, i, i);
|
|
vis[i] = t;
|
|
return;
|
|
}
|
|
|
|
if (diam == 2) {
|
|
int other = parent[center];
|
|
|
|
if (other < center) {
|
|
swap(other, center);
|
|
}
|
|
|
|
vis[other] = t;
|
|
vis[center] = t;
|
|
|
|
ans.emplace_back(diam, other, center);
|
|
return;
|
|
}
|
|
|
|
function<void(int, int)> setvis = [&](int x, int y) {
|
|
int now = x;
|
|
while (now != -1) {
|
|
vis[now] = t;
|
|
now = parent[now];
|
|
}
|
|
|
|
now = y;
|
|
while (now != -1) {
|
|
vis[now] = t;
|
|
now = parent[now];
|
|
}
|
|
};
|
|
|
|
function<void(int, int)> iamtiredmaster = [&](int i, int p) {
|
|
for (auto j : graph[i]) {
|
|
if (j == p) {
|
|
continue;
|
|
}
|
|
|
|
if (vis[j] < t) {
|
|
continue;
|
|
}
|
|
|
|
if (vis[j] == t) {
|
|
iamtiredmaster(j, i);
|
|
continue;
|
|
}
|
|
|
|
solve(j, t + 1);
|
|
}
|
|
};
|
|
|
|
if (diam % 2 == 0) {
|
|
function<pair<int, int>(int, int, int)> dfs = [&](int i, int p, int d) {
|
|
bool leaf = true;
|
|
parent[i] = p;
|
|
|
|
pair<int, int> ans = {-1, -1};
|
|
|
|
for (auto j : graph[i]) {
|
|
if (vis[j] < t || j == p) {
|
|
continue;
|
|
}
|
|
|
|
leaf = false;
|
|
pair<int, int> tmp = dfs(j, i, d + 1);
|
|
|
|
rmax(ans.first, tmp.first);
|
|
rmax(ans.second, tmp.second);
|
|
}
|
|
|
|
if (leaf) {
|
|
if (d == diam / 2) {
|
|
ans.first = i;
|
|
} else if (d == diam / 2 - 1) {
|
|
ans.second = i;
|
|
}
|
|
}
|
|
|
|
return ans;
|
|
};
|
|
|
|
pair<int, int> prev = {-1, -1};
|
|
pair<int, int> final = {0, 0};
|
|
|
|
for (auto j : graph[center]) {
|
|
if (vis[j] < t) {
|
|
continue;
|
|
}
|
|
|
|
pair<int, int> tmp = dfs(j, center, 1);
|
|
pair<int, int> cont1 = {tmp.first, prev.second};
|
|
pair<int, int> cont2 = {tmp.second, prev.first};
|
|
|
|
if (cont1.first < cont1.second) {
|
|
swap(cont1.first, cont1.second);
|
|
}
|
|
|
|
if (cont1.second == -1) {
|
|
cont1 = {-1, -1};
|
|
}
|
|
|
|
if (cont2.first < cont2.second) {
|
|
swap(cont2.first, cont2.second);
|
|
}
|
|
|
|
if (cont2.second == -1) {
|
|
cont2 = {-1, -1};
|
|
}
|
|
|
|
rmax(prev.first, tmp.first);
|
|
rmax(prev.second, tmp.second);
|
|
|
|
rmax(final, cont1);
|
|
rmax(final, cont2);
|
|
}
|
|
|
|
ans.emplace_back(diam, final.first, final.second);
|
|
parent[center] = -1;
|
|
|
|
vis[center] = t;
|
|
setvis(final.first, final.second);
|
|
iamtiredmaster(center, center);
|
|
return;
|
|
}
|
|
|
|
function<int(int, int, int)> dfs = [&](int i, int p, int d) {
|
|
parent[i] = p;
|
|
if (d == diam / 2) {
|
|
return i;
|
|
}
|
|
|
|
int ans = -1;
|
|
|
|
for (auto j : graph[i]) {
|
|
if (vis[j] < t || j == p) {
|
|
continue;
|
|
}
|
|
|
|
rmax(ans, dfs(j, i, d + 1));
|
|
}
|
|
|
|
return ans;
|
|
};
|
|
|
|
priority_queue<int, vi, greater<>> fds;
|
|
fds.push(0);
|
|
fds.push(0);
|
|
|
|
for (auto j : graph[center]) {
|
|
if (vis[j] < t) {
|
|
continue;
|
|
}
|
|
|
|
fds.push(dfs(j, center, 1));
|
|
fds.pop();
|
|
}
|
|
|
|
pair<int, int> tmp;
|
|
tmp.second = fds.top();
|
|
fds.pop();
|
|
tmp.first = fds.top();
|
|
|
|
ans.emplace_back(diam, tmp.first, tmp.second);
|
|
|
|
vis[center] = t;
|
|
parent[center] = -1;
|
|
setvis(tmp.first, tmp.second);
|
|
iamtiredmaster(center, center);
|
|
};
|
|
|
|
solve(0, 0);
|
|
|
|
sort(all(ans), greater<>());
|
|
for (auto [diam, x, y] : ans) {
|
|
cout << diam << ' ' << x + 1 << ' ' << y + 1 << ' ';
|
|
}
|
|
cout << '\n';
|
|
}
|
|
}
|