删除重心后,子树的最大权值最小
dfs遍历每个点,node的所有子树除了它的儿子们还有它往父亲那个方向的一颗子树(权值=总权值-所有子树权值和-1)
下面的代码权值为边权
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 100, inf = 0x3f3f3f3f;
int n, k;
struct edge
{
int to, cost;
edge(int to = 0, int cost = 0) {this->to = to; this->cost = cost;}
};
vector<edge> G[maxn];
int d[maxn];
int Sum = 0;
int Min = inf, MinNode = 0;
void dfs(int node, int par)
{
int Max = 0;
d[node] = 0;
for (auto &ite : G[node])
{
int son = ite.to;
if (son != par)
{
dfs(son, node);
d[node] += ite.cost + d[son];
Max = max(Max, ite.cost + d[son]);
}
}
Max = max(Max, Sum - d[node]);
if (Max < Min)
{
Min = Max;
MinNode = node;
}
}