Tree-shaped DP--HDU 1520 Anniversary party

Source: Internet
Author: User

Test instructions: A university prepares to host the 80 anniversary celebration event. The school's personnel relationship is a tree-shaped structure, the parent node is the boss, sub-node is subordinate. Each node has a value, and the restriction is that the parent node cannot participate in the celebration at the same time (that is, if the parent node participates then his child's node will not be able to participate, if there is a child node to participate, then the Father node can not participate). The maximum value of the value, in the case of the last request to satisfy the limit.

F[n]: Represents the parent node of the I node;

Rating[n]: Value of a node

DP[I][0]: Maximum value of subtree taken with I as root node (I node not taken)

DP[I][1]: Maximum value of subtree taken with I as root node (I node is taken)

State transition equation:

Dp[root][0] + = max (dp[children[root][i]][0], dp[children[root][i]][1]);DP [root][1] + = dp[children[root][i]][0 ];

The final result is one of the maximum value that contains the root node and the maximum value that does not contain the root node.

#include <vector>#include<cstdio>using namespacestd;Const intN =6005;intF[n];intRatings[n];vector<int>Children[n];intdp[n][2];intMaxintAintb) {    returna > B?a:b;}voidDfsintroot) {    intLen =children[root].size (); dp[root][1] =Ratings[root];  for(inti =0; i < Len; ++i)    {DFS (children[root][i]); }     for(inti =0; i < Len; ++i) {dp[root][0] + = max (dp[children[root][i]][0], dp[children[root][i]][1]); dp[root][1] + = dp[children[root][i]][0]; }}intMain () {intN, a, B;  while(SCANF ("%d", &n)! =EOF) {memset (DP,0,sizeof(DP));  for(inti =1; I <= N; ++i) {scanf ("%d", &Ratings[i]); F[i]= -1; }         while(SCANF ("%d%d", &a, &b)) {            if(A = =0&& b = =0) Break; F[a]=b;        Children[b].push_back (a); }        intRoot =1;  while(F[root]! =-1) root =F[root];        DFS (root); printf ("%d\n", Max (dp[root][0], dp[root][1])); }    return 0;}

Tree-shaped DP--HDU 1520 Anniversary party

Related Article

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.