根固定时,就是对于每个非根节点贪心找最小的入边,很显然这样的话最后会构成一个基环树和树的森林,然后把每个环缩点,继续找就好,
void ChuLiu (int n, int m) {
for (int cnt, i, j, _m; ; m = _m, n = cnt) {
for (vis[cnt = 0] = -1, i = 1; i <= n; ++i) pre[i] = -1, vis[i] = bel[i] = 0;
for (i = 1; i <= m; ++i) if (pre[e[i].v] == -1 || w[e[i].v] > e[i].w) w[e[i].v] = e[i].w, pre[e[i].v] = e[i].u;
for (i = 1; i <= n; ++i) if (pre[i] == -1) return (void)puts("GG");
for (i = 1; i <= n; ++i) ans += w[i];
for (i = 1; i <= n; ++i) if (!vis[i]) {
for (j = i; !vis[j]; j = pre[j]) vis[j] = i;
if (vis[j] == i) for (++cnt; !bel[j]; j = pre[j]) bel[j] = cnt;
}
for (i = 1; i <= n; ++i) if (!bel[i]) bel[i] = ++cnt;
if (cnt == n) break;
for (_m = 0, i = 1; i <= m; ++i) if (bel[e[i].u] != bel[e[i].v]) {
++_m;
e[_m].w = e[i].w - w[e[i].v];
e[_m].u = bel[e[i].u], e[_m].v = bel[e[i].v];
}
}
}