Anniversary partyTime 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 is then mutually superior and subordinate (Parent and Child Nodes) cannot be selected at the same time.
Solution: tree-like dp, which can be expanded from one point at will. The dp of all nodes around it can be solved and then added.
Use dp [I] [0] to indicate that no I is selected, the maximum value can be selected for all surrounding areas, 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 is extended from j when I is obtained.
For details, see the code:
Question address: http://acm.hdu.edu.cn/showproblem.php? Pid = 1, 1520
AC code:
# Include
# Include
# Include
# Include
# Include
Using namespace std; const int maxn = 6005; vector
Mq [maxn]; int dp [maxn] [2]; void dfs (int cur, int p) {int I, nex; for (I = 0; I