1 条题解
-
0
#include <bits/stdc++.h> using namespace std; const int N=2e5+10, INF=0x3f3f3f3f; vector<pair<int, int>> G[N]; int n, d[N], deg[N], f[N]; //d[i]记录从i点向下流出的最大流量 //f[i]记录从i点向外流出的最大流量 //deg[i]记录点i的度数 void dfs1(int x, int xfa) { d[x] = 0; for (auto i : G[x]) if (i.first != xfa) { int y = i.first, c = i.second; dfs1(y, x); d[x] += min(c, (deg[y] == 1) ? INF : d[y]); } } void dfs2(int x, int xfa) { for (auto i : G[x]) if (i.first != xfa) { int y = i.first, c = i.second; if (deg[y] == 1) f[y] = min(c, f[x] - c); else f[y] = d[y] + min(c, f[x] - min(d[y], c)); dfs2(y, x); } } int main() { int T; scanf("%d", &T); while (T--) { scanf("%d", &n); memset(G, 0, sizeof(G)); memset(deg, 0, sizeof(deg)); for (int i = 1, x, y, c; i < n; i++) { scanf("%d%d%d", &x, &y, &c); G[x].push_back({y, c}); G[y].push_back({x, c}); deg[x]++, deg[y]++; } dfs1(1, 0); f[1] = d[1]; dfs2(1, 0); int ans = 0; for (int i = 1; i <= n; i++) ans = max(ans, f[i]); printf("%d\n", ans); } return 0; }
- 1
信息
- ID
- 1374
- 时间
- 3000ms
- 内存
- 64MiB
- 难度
- 7
- 标签
- 递交数
- 205
- 已通过
- 54
- 上传者