细节比较多的题。。
看到距离之和最大很容易想到树的直径。
于是我们可以先找出一条直径,先删掉不是这条直径上的点(因为一个点距离最远的点一定是直径的一个端点),最后再删掉这条直径。
按照什么顺序删掉不是直径上的点?我的做法是将这些点按照深度从大到小排序依次删除,因为一个点的深度总是比父亲的深度大,这样可以避免删除非叶节点的情况。
注意这里的深度是指以直径的一个端点为根时的深度,如果像我一样设成以 1 为根时的深度就会 WA on test 28。
然后注意一些细节就行了。
我写的代码比较复杂。。。( exttt{/yun})
#include <bits/stdc++.h>
#define DEBUG fprintf(stderr, "Passing [%s] line %d
", __FUNCTION__, __LINE__)
#define File(x) freopen(x".in","r",stdin); freopen(x".out","w",stdout)
#define DC int T = gi <int> (); while (T--)
using namespace std;
typedef long long LL;
typedef pair <int, int> PII;
typedef pair <int, PII> PIII;
template <typename T>
inline T gi()
{
T f = 1, x = 0; char c = getchar();
while (c < '0' || c > '9') {if (c == '-') f = -1; c = getchar();}
while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return f * x;
}
const int INF = 0x3f3f3f3f, N = 200003, M = N << 1;
int n;
int tot, head[N], ver[M], nxt[M];
int mx, id, dis[N], pre[N];
bool vis[N];
int fa[N], dep[N], son[N], sz[N], topp[N];
int seq[N], cnt;
vector <int> zj;
struct Ans {int a, b, c;};
vector <Ans> Program;
int dep1[N];
inline void add(int u, int v) {ver[++tot] = v, nxt[tot] = head[u], head[u] = tot;}
inline void bfs(int u)
{
queue <int> q;
q.push(u);
memset(dis, 0, sizeof dis);
memset(vis, false, sizeof vis);
memset(pre, 0, sizeof pre);
vis[u] = true;
mx = id = 0;
while (!q.empty())
{
int u = q.front(); q.pop();
for (int i = head[u]; i; i = nxt[i])
{
int v = ver[i];
if (vis[v]) continue;
vis[v] = true;
dis[v] = dis[u] + 1;
pre[v] = u;
if (dis[v] > mx) mx = dis[v], id = v;
q.push(v);
}
}
}
void dfs_dep(int u, int f)
{
dep1[u] = dep1[f] + 1;
for (int i = head[u]; i; i = nxt[i])
{
int v = ver[i];
if (v == f) continue;
dfs_dep(v, u);
}
}
inline bool cmp(int x, int y) {return dep1[x] > dep1[y];}
namespace LCA
{
void dfs1(int u, int f)
{
sz[u] = 1, dep[u] = dep[f] + 1, fa[u] = f;
for (int i = head[u]; i; i = nxt[i])
{
int v = ver[i];
if (v == f) continue;
dfs1(v, u);
sz[u] += sz[v];
if (sz[v] > sz[son[u]]) son[u] = v;
}
}
void dfs2(int u, int f)
{
topp[u] = f;
if (!son[u]) return; dfs2(son[u], f);
for (int i = head[u]; i; i = nxt[i])
{
int v = ver[i];
if (v == fa[u] || v == son[u]) continue;
dfs2(v, v);
}
}
int getLCA(int u, int v)
{
while (topp[u] != topp[v])
{
if (dep[topp[u]] < dep[topp[v]]) swap(u, v);
u = fa[topp[u]];
}
if (dep[u] < dep[v]) return u;
return v;
}
} //namespace LCA
int main()
{
//File("");
n = gi <int> ();
for (int i = 1; i < n; i+=1)
{
int u = gi <int> (), v = gi <int> ();
add(u, v), add(v, u);
}
bfs(1);
int lft = id;
bfs(lft);
int rght = id;
LCA :: dfs1(1, 0); LCA :: dfs2(1, 1);
memset(vis, false, sizeof vis);
int now = rght; vis[now] = true; zj.push_back(now);
while (now != lft) now = pre[now], vis[now] = true, zj.push_back(now);
for (int i = 1; i <= n; i+=1) if (!vis[i]) seq[++cnt] = i;
dfs_dep(lft, 0);
sort(seq + 1, seq + 1 + cnt, cmp);
LL ans = 0;
for (int i = 1; i <= cnt; i+=1)
{
int u = seq[i];
int lca1 = LCA :: getLCA(u, lft), lca2 = LCA :: getLCA(u, rght);
int dis1 = dep[u] + dep[lft] - 2 * dep[lca1], dis2 = dep[u] + dep[rght] - 2 * dep[lca2];
if (dis1 >= dis2) ans += dis1, Program.push_back((Ans){u, lft, u});
else ans += dis2, Program.push_back((Ans){u, rght, u});
}
for (int i = 0; i < (int)zj.size() - 1; i+=1)
ans += mx - i, Program.push_back((Ans){zj[i], lft, zj[i]});
printf("%lld
", ans);
for (int i = 0; i < n - 1; i+=1) printf("%d %d %d
", Program[i].a, Program[i].b, Program[i].c);
return 0;
}