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): 2124 Accepted Submission (s): 896

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 Input7 1 1 1 1 1 1 1 1 3 2 3 6 4 4 4 5 5 0

 

Sample Output5

 

SourceUral State University Internal Contest October '2000 Students Session

 

Recommendlinle's simple tree DP entry question creates a directed graph with the vector in STL, and then creates an undirected graph with the struct. Two programs
/* HDU 1540 simple tree DPSTL vector realize chain table build G ++ 125 MS 784 K */# include <stdio. h> # include <string. h> # include <iostream> # include <vector> # include <algorithm> using namespace std; const int MAXN = 6050; vector <int> vec [MAXN]; int f [MAXN]; int hap [MAXN]; int dp [MAXN] [2]; void dfs (int root) {int len = vec [root]. size (); dp [root] [1] = hap [root]; for (int I = 0; I <len; I ++) dfs (vec [root] [I]); for (int I = 0; I <len; I ++) {dp [root] [0] + = max (dp [vec [Root] [I] [1], dp [vec [root] [I] [0]); dp [root] [1] + = dp [vec [root] [I] [0] ;}} int main () {// freopen ("in.txt ", "r", stdin); // freopen ("out.txt", "w", stdout); int n; int a, B; while (scanf ("% d ", & n )! = EOF) {for (int I = 1; I <= n; I ++) {scanf ("% d", & hap [I]); vec [I]. clear (); f [I] =-1; // The root flag dp [I] [0] = dp [I] [1] = 0 ;} while (scanf ("% d", & a, & B) {if (a = 0 & B = 0) break; f [a] = B; vec [B]. push_back (a);} a = 1; while (f [a]! =-1) a = f [a]; // find the root dfs (a); printf ("% d \ n", max (dp [a] [1], dp [a] [0]);} return 0 ;}

 

 

/* HDU 1540 simple tree-shaped DP struct to build a chain table G ++ 109 ms 416 K */# include <stdio. h> # include <string. h ># include <algorithm> using namespace std; const int MAXN = 6010; struct Node {int v; Node * next ;}; Node * head [MAXN]; // The header pointer Node edge [MAXN * 2]; // This must be a larger int tol; // The total number of edges, that is, the edge array int dp [MAXN] [2]; int hap [MAXN]; bool vis [MAXN]; void init () {tol = 0; memset (dp, 0, sizeof (dp); memset (head, NULL, sizeof (head); // initialize the head pointer memset (vis, false, sizeof (vis ));} Void add_edge (int a, int B) // Add an undirected edge between a and B {edge [tol]. v = B; edge [tol]. next = head [a]; head [a] = & edge [tol ++]; edge [tol]. v = a; edge [tol]. next = head [B]; head [B] = & edge [tol ++];} void dfs (int v) {if (vis [v]) return; vis [v] = true; Node * p = head [v]; dp [v] [1] = hap [v]; while (p! = NULL) {if (! Vis [p-> v]) {dfs (p-> v); dp [v] [0] + = max (dp [p-> v] [0], dp [p-> v] [1]); dp [v] [1] + = dp [p-> v] [0];} p = p-> next;} int main () {// freopen ("in.txt", "r", stdin); // freopen ("out.txt ", "w", stdout); int n, a, B; while (scanf ("% d", & n )! = EOF) {init (); for (int I = 1; I <= n; I ++) scanf ("% d", & hap [I]); while (scanf ("% d", & a, & B) {if (a = 0 & B = 0) break; add_edge (, b);} // as an undirected graph is created, you can find any vertex to perform DP on the root of the tree. // However, you need to determine the weight in the search and add a vis array dfs (1 ); printf ("% d \ n", max (dp [1] [0], dp [1] [1]);} 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.