Title Link: Poj 2342
The main idea: a company to hold a party, but in order to make the atmosphere of the party more active, each party will not want to see his direct boss at the party, now known to everyone's active index and boss relationship (of course, there is no link), ask who (how many people) to invite who can make the party's total active index maximum.
Idea: Since it is a tree-shaped DP, it is natural to build a tree, here is the adjacency table (l<-k). When looking for root, the flag array is used to mark all the sons nodes, so the non-marked nature is the root node. The tree DP starts from the leaf node, so it searches deep into the leaf node and then goes back to the parent node. In the processing of the son node, the parent node does not participate in the words of the son node, take away and not to go in the maximum value; Father node participation then the Son node cannot go. Here the definition dp[][2],0 means not to go, 1 means to go. See the code for specific use .....
/************************************************************** problem:poj 2342 user:youmi language:c++ R esult:accepted time:16ms memory:408k****************************************************************///#pragma COMMENT (linker, "/stack:1024000000,1024000000")//#include <bits/stdc++.h>#include <iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<map>#include<stack>#include<sstream>#include<cmath>#include<queue>#include<string>#include<vector>#defineZeros (a) memset (A,0,sizeof (a))#defineOnes (a) memset (A,-1,sizeof (a))#defineSC (a) scanf ("%d", &a)#defineSC2 (A, b) scanf ("%d%d", &a,&b)#defineRep0 (i,n) for (int i=0;i<n;i++)#defineREP1 (i,n) for (int i=1;i<=n;i++)#defineMax (a) (a) > (b)? (a):(B)#defineMin (a) (a) < (b)? (a):(B)#definePT (a) printf ("%d\n", a)#defineLson (step<<1)#defineRson (lson+1)#defineESP 1e-6#defineOO 0x3fffffff#defineTEST cout<< "*************************" <<endlusing namespaceStd;typedefLong Longll;intN;Const intmaxn=6000+Ten;intdp[maxn][2];intVIS[MAXN];intHEAD[MAXN];intFLAG[MAXN];intT;structside{intV,next;} E[MAXN];voidinit () {T=0; Zeros (VIS); Zeros (DP); Zeros (flag); Ones (head);}voidBuildintUintv) {E[T].V=v; E[t].next=Head[u]; Head[u]=t++;}voidTREE_DP (introot) {Vis[root]=1; //pt (root); for(intI=head[root];~i;i=E[i].next) { intv=e[i].v; if(!Vis[v]) {TREE_DP (v); dp[root][1]+=dp[v][0]; dp[root][0]+=max (dp[v][1],dp[v][0]); } }}intMain () {//freopen ("In.txt","R", stdin); /**< int T_T;SC (T_T); for (int kase=1;kase<=t_t;kase++)*/ //While (~SC (n))SC (n); {init (); intRoot; REP1 (i,n) {SC (dp[i][1]); } intu,v; REP1 (I,n-1) {SC2 (u,v); //printf ("%d%d\n", u,v);build (V,u); Flag[u]=1; } rep1 (i,n) {if(!Flag[i]) {Root=i; Break; } } //pt (root);TREE_DP (root); PT (Max (dp[root][1],dp[root][0])); } return 0;}
Tree-shaped DP POJ 2342