Start by thinking that all edges are a separate path
And then try to merge as much as possible
1#include <cstdio>2#include <cstring>3#include <cstdlib>4#include <algorithm>5#include <iostream>6 7 using namespacestd;8 9 voidSetio (Const string&s) {TenFreopen ((S +". in"). C_STR (),"R", stdin); OneFreopen ((S +". out"). C_STR (),"W", stdout); A } -Template<typename q> Q Read (q&x) { - Static Charc, F; the for(f =0; c = GetChar (),!isdigit (c); )if(c = ='-') F =1; - for(x =0; IsDigit (c); c = GetChar ()) x = x *Ten+ C-'0'; - if(f) x =-x; - returnx; + } -Template<typename q>Q Read () { + StaticQ x; Read (x);returnx; A } at - Const intN =100000+Ten; - intMain () { - #ifdef DEBUG -Freopen ("In.txt","R", stdin); -Freopen ("OUT.txt","W", stdout); in #endif - to Static intDu[n], mx[n]; + intT = read<int>(); - for(intCAS =1; CAS <= T; cas++) { theMemset (Du,0,sizeofdu); *memset (MX,0,sizeofMX); $ intAns =0;Panax Notoginseng intU, V, W, n = read<int>(); - for(inti =1; I < n; i++) { the Read ( u), read (v), read (w); +Du[u] + = W, du[v] + =W; AMx[u] =Max (Mx[u], W); theMX[V] =Max (Mx[v], W); +Ans + =W; - } $ $ for(intU =1; U <= N; u++) { - if((Mx[u) <<1) <= Du[u]) ans-= Du[u] >>1; - ElseAns-= du[u]-Mx[u]; the } - Wuyiprintf"Case #%d:%d\n", CAs, ans); the } - Wu return 0; -}
View Code
UVaLive6039 Uva1668 let ' s Go Green