hdu1520 Anniversary Party (poj2342, tree DP)

Source: Internet
Author: User

Anniversary partyTime limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 7303 Accepted Submission (s): 3220


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:
L 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 Input
711111111 32 36 47 44 53 50 0

Sample Output
5

Sourceural State University Internal Contest October ' Students Session
This problem and poj2342 is the same, but HDU data strengthened, with POJ2342AC code may time out, poj2342 parsing report here is a little different, but probably also is the same, just joined some father and son brother relations, is not difficult to understand.
#include <iostream> #include <cstdio> #include <cstring> #include <stack> #include <queue > #include <map> #include <set> #include <vector> #include <cmath> #include <algorithm> Using namespace Std;const double eps = 1e-6;const double pi = ACOs ( -1.0); const int INF = 0x3f3f3f3f;const int MOD = 100000 0007; #define LL Long Long#define CL (A, B) memset (A,b,sizeof (a)) struct tree{int father;//father int child;//son int bro ther;//Brother Int not;//don't go party int takeparty;//go Party int MAX () {return takeparty > not? Takeparty:not;}    struct Call function save time int init ()//tree Empty {father = Child = Brother = not = 0;    }}tree[6005];void dfs (int idx) {int child = Tree[idx].child;        while (child)//If there are children continue to check {DFS; TREE[IDX]. Takeparty + = Tree[child]. not;//if the IDX goes to the party, then its children will not be able to go to tree[idx]. Not + = Tree[child].  MAX ();//If IDX does not go to party, then its children can go without child = tree[child].brother;//Find other children}}int main () {int n,a,b;  while (scanf ("%d", &n) ==1) {for (int i=1; i<=n; i++) {scanf ("%d", &tree[i].            Takeparty);        Tree[i].init ();            } while (scanf ("%d%d", &a,&b), a+b)//achievements {tree[a].father = B;            Tree[a].brother = Tree[b].child;        Tree[b].child = A;                } for (int i=1; i<=n; i++)//Find {if (!tree[i].father) {DFS (i); printf ("%d\n", Tree[i].                MAX ());            Break }}} return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

hdu1520 Anniversary Party (poj2342, tree DP)

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.