如果你环上的 dp 值正确,但是求非环上换根 dp 的值错了,并且确认换根本身没有错,可能是没有更新环上节点的 sonsonson 值(就是子节点数量),应该加上 222。
for (int i = 1; i <= tot; i++) { f[cir[i].first] = sum[cir[i].first]; son[cir[i].first] += 2; // here dfs2(cir[i].first, 0); }