POJ 2342 Anniversary party (getting started with tree dp)
Anniversary party
Time Limit:1000 MS |
|
Memory Limit:65536 K |
Total Submissions:4810 |
|
Accepted:2724 |
Description
There 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.
Input
Employees 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 N-1 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
Output
Output shoshould contain the maximal sum of guests ratings.
Sample Input
711111111 32 36 47 44 53 50 0
Sample Output
5
Source
Ural State University Internal Contest October '2000 Students Session
Question link: http://poj.org/problem? Id = 2342
A tree has a value for each node. Now we need to select some vertices from them. These vertices must have the highest value and maximum value, and each pair of sons and fathers cannot be selected at the same time.
Question Analysis: dp [I] [0] and dp [I] [1] indicate the sum of the subtree values when I is the sub-tree root when I is not selected and I is selected, respectively. Then:
Dp [fa [I] [1] + = dp [I] [0] indicates that when I is selected as the father, its value is equal to its own value plus the value when I is not selected
Dp [fa [I] [0] + = max (dp [I] [1], dp [I] [0]) indicates that if the father is not selected, the value is equal to the maximum value of the selected or unselected value of the son.
# Include
# Include
# Include
# Include using namespace std; int const MAX = 6005; int dp [MAX] [2], val [MAX]; bool vis [MAX], flag [MAX]; vector
Vt [MAX]; void DFS (int fa) {vis [fa] = true; dp [fa] [1] = val [fa]; int sz = vt [fa]. size (); for (int I = 0; I <sz; I ++) {int son = vt [fa] [I]; if (! Vis [son]) {DFS (son); dp [fa] [1] + = dp [son] [0]; dp [fa] [0] + = max (dp [son] [1], dp [son] [0]) ;}} return ;} int main () {int n; while (scanf ("% d", & n) {for (int I = 1; I <= n; I ++) vt [I]. clear (); memset (flag, false, sizeof (flag); memset (vis, false, sizeof (vis); memset (dp, 0, sizeof (dp )); for (int I = 1; I <= n; I ++) scanf ("% d", & val [I]); for (int I = 0; I <n-1; I ++) {int a, B; scanf ("% d", & ,& B); vt [B]. push_back (a); flag [a] = true;} int root; for (int I = 1; I <= n; I ++) // n-1 edge, there must be a point with "inbound" 0, that is, the root of the tree {if (! Flag [I]) {root = I; break;} DFS (root); printf ("% d \ n", max (dp [root] [1], dp [root] [0]) ;}}