HDU 1520 anniversary party (simple tree DP)

Source: Internet
Author: User
Anniversary Party Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 4310 accepted submission (s): 1976


Problem descriptionthere is going to be a party to celebrate the 80-th Anniversary of the Ural State University. the University has a hierarchical structure of employees. it means that the supervisor relation forms a tree rooted at the Rector v. e. tretyakov. in order to make the party funny for every one, the Rector does not want both an employee and his or her immediate supervisor to be present. the Personnel Office has evaluated conviviality of each employee, so everyone has some number (rating) attached to him or her. your task is to make a list of guests with the maximal possible sum of guests 'conviviality ratings.
 
Inputemployees are numbered from 1 to n. A first line of input contains a number n. 1 <= n <= 6 000. each of the subsequent n lines contains the conviviality rating of the corresponding employee. conviviality rating is an integer number in a range from-128 to 127. after that go t lines that describe a supervisor relation tree. each line of the tree specification has the form:
L K
It means that the k-th employee is an immediate supervisor of the L-th employee. input is ended with the line
0 0
Outputoutput shoshould contain the maximal sum of guests ratings.
 
Sample Input
711111111 32 36 47 44 53 50 0
 
Sample output
5
 
Sourceural state university internal Contest October '2000 students session
Recommendlinle | we have carefully selected several similar problems for you: 1561 1011 2196 1494

I will give you a tree of interpersonal relationships, which can then be selected at different levels (Parent and Child Nodes.
Solution: tree-like DP, which can be expanded at any time starting from one point, and the DP of all nodes around it can be solved.
Use DP [I] [0] to indicate the maximum value that can be selected if no I is selected, and DP [I] [1] to indicate that no I is selected, the maximum value cannot be selected. Then
DP [I] [0] + = (DP [J] [1], DP [J] [0])
DP [I] [1] + = DP [J] [0]
I is adjacent to J, And I has been obtained from J extension.

For details, see the code:
Question address: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 1520
AC code:
# Include <iostream> # include <cstdio> # include <cstring> # include <string> # include <vector> using namespace STD; const int maxn = 6005; vector <int> MQ [maxn]; int DP [maxn] [2]; void DFS (INT cur, int p) {int I, NEX; for (I = 0; I <MQ [cur]. size (); I ++) {NEX = MQ [cur] [I]; If (NEX = P) continue; DFS (NEX, cur ); // find the extended DP [cur] [0] + = max (DP [NEX] [0], DP [NEX] [1]) from the cur point. DP [cur] [1] + = DP [NEX] [0] ;}} int main () {int N, I; int A, B; while (~ Scanf ("% d", & N) {memset (DP, 0, sizeof (DP); for (I = 1; I <= N; I ++) {MQ [I]. clear (); scanf ("% d", & DP [I] [1]);} while (scanf ("% d", & A, & B) & (a + B) {MQ [A]. push_back (B); MQ [B]. push_back (a);} DFS (1, 0); int ans = max (DP [1] [0], DP [1] [1]); 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.