First of all, there is no map, because a does not look at B they will not be selected together ...
Then, n points n edges, forming a ring bush tree forest
So there is a artifice, Dfs first find two points on the ring, and then remove the edge, write down the sides of the X, y two nodes
Force X to root and x do not choose to do the tree, and then force Y to root and y to do the tree motion again, two times the maximum added answer
Pay attention to the wording, I recorded the father can not find the case of the two Yuan ring Qaqqq,wa to death ... The side you want to record.
1 /**************************************************************2 problem:10403 User:rausen4 language:c++5 result:accepted6 time:1992 Ms7 memory:51724 KB8 ****************************************************************/9 Ten#include <cstdio> One#include <algorithm> A - using namespacestd; - Const intN = 1e6 +5; the - structEdge { - intnext, to, F; - Edge () {} +Edgeint_n,int_t,int_f =1): Next (_n), to (_t), F (_f) {} -} e[n <<1]; + A intN, Rt1, Rt2; at intFirst[n], tot =1; - intV[n], vis[n]; - Long Longf[n][2], ans; - -InlineintRead () { - intx =0; in CharCH =GetChar (); - while(Ch <'0'||'9'<ch) toCH =GetChar (); + while('0'<= CH && Ch <='9') { -x = x *Ten+ CH-'0'; theCH =GetChar (); * } $ returnx;Panax Notoginseng } - theInlinevoidAdd_edges (intXinty) { +E[++tot] = Edge (first[x], y), first[x] =tot; AE[++tot] = Edge (First[y], x), first[y] =tot; the } + - #defineY e[x].to $ voidDfsintPint from) { $ intx; -VIS[P] =1; - for(x = first[p]; x; x =e[x].next) the if(!Vis[y]) dfs (y, x); - Else if(( from^1)! = x &&!rt1)WuyiRt1 = p, Rt2 = y, e[x].f = e[x ^1].F =0; the } - Wu voiddpintPintt) { - intx; AboutVIS[P] =T; $f[p][0] =0, f[p][1] =V[p]; - for(x = first[p]; x; x =e[x].next) - if(Vis[y]! = t &&e[x].f) { - dp (y, t); Af[p][0] + = max (f[y][0], f[y][1]); +f[p][1] + = f[y][0]; the } - } $ #undefY the the intMain () { the inti; the Long Longtmp; -n =read (); in for(i =1; I <= N; ++i) theV[i] =read (), add_edges (read (), i); the for(i =1; I <= N; ++i) About if(!Vis[i]) { theRt1 = Rt2 =0; theDFS (I,0); theDP (RT1,2), TMP = f[rt1][0]; +DP (RT2,3), ans + = MAX (tmp, f[rt2][0]); - } theprintf"%lld\n", ans);Bayi return 0; the}View Code
BZOJ1040 [ZJOI2008] Knight