POJ 2342 Anniversary Party (tree-shaped DP)

Source: Internet
Author: User

Anniversary Party
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 7230 Accepted: 4162
DescriptionThere 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. 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 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 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 Ideas
Test Instructions: A company to hold a party, but in order to make the atmosphere of the party more active, every person who took part in the redemption do not want to see their direct boss. It is now known that each person's active index and boss relationship (no ring), the maximum value of the active index of the party.
thought:Dp[x][0] said X go to the party, Dp[x][1] said x don't go to the party. Then there are two scenarios:
    • X go to the party, then his direct subordinates y can't attend the party, dp[x [1] = dp[y [0]
    • x do not go to the party, his direct subordinate Y can participate or not to participate, dp[x [0] = max (dp[y [0],dp[y] [1])

/*dp[x][0]:x not go to the party dp[x][1]:x go to the party * * #include <iostream> #include <cstdio> #include <cstring> #include <algorithm>using namespace std;const int maxn = 6005;int tot = 0,fa[maxn],head[maxn],dp[maxn][2];; struct Tree{int fa,son;int next; Tree (): FA (0), son (0), next (0) {}}tree[maxn];void addedge (int u,int v) {Tree[tot].son = V;tree[tot].next = Head[u];head[u] = tot++;} void dfs (int cur) {for (int i = head[cur];i! = -1;i = Tree[i].next) {dfs (Tree[i].son);DP [cur][1] + = dp[tree[i].son][0];DP [cu R][0] + = max (dp[tree[i].son][0],dp[tree[i].son][1]);}} int main () {int n;while (~scanf ("%d", &n)) {memset (head,-1,sizeof (head)); Memset (Dp,0,sizeof (DP)); for (int i = 1;i <= n;i++) scanf ("%d", &dp[i][1]), int l,k;while (scanf ("%d%d", &l,&k) && L && K) {tree[l]. FA = K;addedge (k,l);} int root = 1;while (tree[root].fa) root = Tree[root].fa;dfs (root);p rintf ("%d\n", Max (dp[root][0],dp[root][1]));} return 0;} 

  

  

POJ 2342 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.