1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
| #include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #define MAXN 200005 using namespace std;
struct Edge {int next, to, flow;} edge[MAXN*2];
int T, n, x, y, z, ans, cnt, f[MAXN], g[MAXN], deg[MAXN], head[MAXN];
void addedge(int from, int to, int flow) { edge[++cnt].next=head[from]; edge[cnt].to=to; edge[cnt].flow=flow; deg[to]++; head[from]=cnt; }
void init() { memset(edge, 0, sizeof(edge)); for(int i=1; i<MAXN; i++) f[i]=g[i]=head[i]=deg[i]=0; cnt=ans=0; }
void dfs1(int x, int fa) { for(int i=head[x]; i; i=edge[i].next) { int to=edge[i].to; if(to==fa) continue; dfs1(to, x); if(deg[to]==1) f[x]+=edge[i].flow; else f[x]+=min(f[to], edge[i].flow); } }
void dfs2(int x, int fa) { for(int i=head[x]; i; i=edge[i].next) { int to=edge[i].to; if(to==fa) continue; if(deg[x]==1) g[to]=f[to]+edge[i].flow; else g[to]=f[to]+min(g[x]-min(f[to], edge[i].flow), edge[i].flow); dfs2(to, x); } ans=max(ans, g[x]); }
int main() { scanf("%d", &T); while(T--) { init(); scanf("%d", &n); for(int i=1; i<n; i++) { scanf("%d%d%d", &x, &y, &z); addedge(x, y, z); addedge(y, x, z); } dfs1(1, 0); g[1]=f[1]; dfs2(1, 0); printf("%d\n", ans); } }
|