HDOJ 1520&poj2342 Anniversary Party "tree-shaped DP"

Source: Internet
Author: User

Anniversary party

Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 5917 Accepted Submission (s): 2692

Problem Description
There is going-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.

Input
Employees 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

Output
Output should contain the maximal sum of guests ' ratings.

Sample Input
7
1
1
1
1
1
1
1
1 3
2 3
6 4
7 4
4 5
3 5
0 0

Sample Output
5

Test instructions: There is a party to invite a company of people, the company has a strict hierarchy, is a tree, now each node of the tree has a (rating) weight, requires an employee can not and his immediate boss (parent node) at the same time, to be able to appear the maximum weight and.
Idea: Because the maximum value of the preceding node is taken into account the maximum value of the subsequent byte point, use DP.
State equation: Dp[i] "0" (representing the non-occurrence of the node i), dp[i][1] indicates the presence of a.
Then dp[i][0] + = max (dp[j][0], dp[j][1]);
DP[I][1] + = dp[j][0]; Where j is the child node of I.
The code (HDOJ's data is not particularly water without vector will TL):

#include <cstdio>#include <cstring>#include <algorithm>#include <vector>using namespace STD;Const intM =6e3+5;intdp[m][2], fat[m], N;BOOLVIS[M]; vector<int > Map[M];voidDfsintu) {Vis[u] =1; for(inti =0; I <Map[U].size (); + + i) {intv =Map[U] [i];if(!vis[v])            {DFS (v); dp[u][0] + = max (dp[v][0], dp[v][1]); dp[u][1] + = dp[v][0]; }    }}intMain () { while(scanf("%d", &n) = =1){ for(inti =1; I <= N; ++i) {dp[i][0] = dp[i][1] =0;            Fat[i] = i; Vis[i] =0;Map[I].clear (); } for(inti =1; I <= N; ++i) {scanf("%d", &dp[i][1]); }intL, K; while(scanf("%d%d", &l, &k), l| | K) {if(Fat[l] = = L) {Fat[l] = k;Map[K].push_back (L); }        }intRoot =1; while(root = fat[root]) root = Fat[root];//memset (Vis, 0, sizeof (VIS));DFS (root);printf("%d\n", Max (dp[root][0], dp[root][1])); }return 0;}

HDOJ 1520&poj2342 Anniversary Party "tree-shaped 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.