"Tree dp" HDU 4303 Hourai Jeweled

Source: Internet
Author: User

Channel

Test instructions: Give a tree, the side has color, point the right value. A path that satisfies no two adjacent edges on a path is a good path, and the sum of the paths (the weights and the points on the path)

Ideas:

Benquan sort, starting at any point, deep search, after each subtree is searched, returns the number of pair< that can be extended to this point and the last edge is different from the edge color of the parent node to that point, all of which are the sum of the edge fractions >
After each deep search for a child node, the total score of the gorgeous edge added to this point is:
The sum of the number of edges returned by all the child nodes that were previously searched up * The score of the current child node +
The sum of the points returned by all the child nodes that were previously searched upward * The number of edges returned by the current child node +
The sum of the number of edges returned by all the child nodes that were previously searched * The number of edges returned by the current child node * Right of the current point

Code:

#include <cstdio>#include<cstring>#include<algorithm>using namespacestd;Const intMax_n =300007; typedefLong Longll;structEdge {intu, V, W, NXT; Edge () {} Edge (int_u,int_v,int_w,int_n) {u= _u; v = _v; W = _w; NXT =_n; }    BOOL operator< (ConstEdge &AMP;RHS)Const {        returnW <RHS.W; }};intN;intHead[max_n], ecnt;ll ans, val[max_n], Num[max_n], sum[max_n]; Edge T[max_n], g[max_n<<1];voidClear () {ecnt= ans =0; Memset (Head,-1,sizeofhead); memset (num,0,sizeofNUM); memset (SUM,0,sizeofsum);}voidAddintUintVintW) {g[ecnt]=Edge (U, V, W, Head[u]); Head[u]= ecnt++;}voidDfsintUintFaintCol) {ll tot=0, tot1 =0, Tot2 =0, k =0, K1 =0, K2 =0; intNowcol =0;  for(inti = Head[u]; ~i; i =g[i].nxt) {        intv = g[i].v, w =G[I].W; if(v = = FA)Continue;        DFS (V, u, W); if(w! = col) tot + = Sum[v], k + =Num[v]; if(W! =Nowcol) {TOT1+ = Tot2; K1 + =K2; Tot2= Sum[v], K2 =Num[v]; Nowcol=W; } ElseTot2 + = Sum[v], K2 + =Num[v]; Ans+ = sum[v] * (k1 +1) + tot1 * Num[v] + val[u] * (k1 +1) *Num[v]; } Num[u]= ++k, Sum[u] = val[u] * k +tot;} Template<classT>inlineBOOLRD (T &ret) {    CharCintSGN; if(c = GetChar (), c = = EOF)return false;  while(c! ='-'&& (C <'0'|| C >'9')) C =GetChar (); SGN= (c = ='-') ? -1:1; RET= (c = ='-') ?0: (C-'0');  while(c = GetChar (), C >='0'&& C <='9') ret = RET *Ten+ (C-'0'); RET*=SGN; return true;}intMain () { while(1= = scanf ("%d", &N)) { for(inti =1; I <= N; ++i) Rd (Val[i]);  for(inti =0; i +1< n; ++i) {rd (t[i].u), RD (T[I].V), RD (T[I].W); } sort (T, T+ N-1);        Clear ();  for(inti =0; i +1< n; ++i) {Add (t[i].u, T[I].V, T[I].W);        Add (T[I].V, t[i].u, T[I].W); } DFS (1, -1, -1); printf ("%i64d\n", ans); }    return 0;}
View Code

Tree dp HDU 4303 Hourai Jeweled

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.