Anniversary Party
Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 6990 Accepted Submission (s): 3104
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 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 a employee and his or her immediate supervi Sor to is present. The personnel office has evaluated conviviality of all employee, so everyone had 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 is 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 the go T lines that describe a supervisor relation tree. Each line of the tree specification has the form:
K
It means that the k-th employee was an immediate supervisor of the L-th employee. Input is ended with the line
0 0
Outputoutput should contain the maximal sum of guests ' ratings.
Sample Input711111111 32 36 47 44 53 50 0
Sample OUTPUT5
Sourceural State University Internal Contest October ' Students Session
Test instructions: N individuals (numbered 1-n), then n rows of tables represent the activity of the nth person, followed by a number of lines L and K (0 0 end), representing K is the boss of L,
Ask a party to invite a few of these n individuals, which cannot contain direct subordinate relationships, which can make the maximum amount of activity in a party.
1#include <stdio.h>2#include <string.h>3#include <stdlib.h>4#include <iostream>5#include <algorithm>6#include <vector>7 #definell Long Long8 using namespacestd;9 Const intN =10000;Ten intdp[n][3],root[n]; Onevector<int>G[n]; A voidDFS (intcur) - { - for(inti =0; I < (int) G[cur].size (); i++) the { - intto =G[cur][i]; - DFS (to); -dp[cur][1] + = dp[to][0];//the level goes, the next level does not go +dp[cur][0] + = max (dp[to][0],dp[to][1]);//the level does not go, subordinates go not to choose the best - } + } A at intMainvoid) - { - intN; - while(SCANF ("%d", &n)! =EOF) - { -Memset (DP,0,sizeof(DP)); in for(inti =1; I <= N; i++) - { toscanf"%d", &dp[i][1]);//on behalf of the first few people to be able to increase the activity of +Root[i] = i;//and the initialization of the check set - g[i].clear (); the } * intb; $ while(SCANF ("%d%d", &a,&b), A! =0|| B! =0)Panax Notoginseng { -G[b].push_back (a);//Vector Building Map theRoot[a] =b; + } A intBeg; the for(inti =1; I <= N; i++) + { - if(Root[i] = = i)//Find root node $ { $Beg =i; - Break; - } the } - DFS (beg);Wuyiprintf"%d\n", Max (dp[beg][0],dp[beg][1])); the } - return 0; Wu}
HDU OJ 1520 Anniversary Party (tree DP Primer)