Hdu_1520 anniversary praty (tree DP)

Source: Internet
Author: User
/* Question: the legendary Ural God school will celebrate its 80 th anniversary. The school's staff relationship is a tree where each employee has
Pleasure value, indicating the degree of fun to participate in the party. Every employee does not want to join his or her boss.
Party. Then the principal thought about how to send invitations to make the party the highest.

Idea: Define FW [I] to indicate the maximum pleasure of the subtree when the root node is added, and FN [I] to indicate that the root node is not included.
The maximum pleasure of a subtree. The transfer equation is:

FW [I] = sum (FN [son [I]);

FN [I] = sum (max (FN [son [I], FW [son [I]);

In fact, it is easy to implement and draw a picture at a glance. */

// PS: 1y, ^ _ ^

// My code:

# Include <iostream>
# Include <cstdio>
# Include <cstring>
# Include <vector>

Using namespace STD;

Const int n= 6010;

Vector <int> G [N];

Int FW [N], FN [N], p [N];
Bool vis [N];

Void DFS (int r ){
If (vis [R]) return;
Vis [R] = true;

Int Len = G [R]. Size ();
If (! Len ){
FN [R] = 0;
FW [R] = P [R];
Return;
}
Int I, C;
For (I = 0; I <Len; I ++ ){
C = G [r] [I];
DFS (C );
FN [R] + = max (FN [c], FW [c]);
FW [R] + = FN [c];
}
FW [R] + = P [R];
}

Int main (){
// Freopen ("data. In", "r", stdin );

Int n, x, y, I, ans;
While (~ Scanf ("% d", & N )){
For (I = 0; I <= N; I ++) g [I]. Clear ();

For (I = 1; I <= N; I ++ ){
Scanf ("% d", P + I );
}

While (scanf ("% d", & X, & Y), X | Y ){
G [Y]. push_back (X );
}

Memset (FN, 0, sizeof (FN ));
Memset (FW, 0, sizeof (FW ));
Memset (VIS, false, sizeof (VIS ));

For (I = 1; I <= N; I ++ ){
If (! Vis [I]) DFS (I );
}

For (ANS = 0, I = 1; I <= N; I ++ ){
Ans = max (ANS, max (FW [I], FN [I]);
}
Printf ("% d \ n", ANS );
}
Return 0;
}

 

 

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.