10.8 树形dp
G1. Min-Fund Prison (Easy)
问题 - G1 - Codeforces — Problem - G1 - Codeforces
树形dp。
题意:给一颗树,切去一边得到两个联通块x y,求 $ x^2* y^2$ 最小。
思路:枚举每棵子树作为断开联通块,取最小值。
void solve()
{cin >> n >> m >> k;while (m--){cin >> x >> y;e[x].emplace_back(y);e[y].emplace_back(x);}ans = 1e18;auto dfs = [&](auto self, auto u, int fa) -> void{a[u] = 1;for (auto i : e[u]){if (i == fa) //双向图,如果是父节点就跳过{continue;}self(self, i, u);ans = min(ans, (n - a[i]) * (n - a[i]) + a[i] * a[i]);a[u] += a[i];}};dfs(dfs, 1, 0);cout << ans;rep(1, i, n){e[i].clear();a[i] = 0;}
}