【Poj】 p2342 Anniversary party(树形DP第一道)

Anniversary party

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 5523   Accepted: 3169

Description

There 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.

Input

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

Source

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 using namespace std;
 5 int n;
 6 int f[6010][2],used[6010],father[6010];
 7 void treee(int num)
 8 {
 9     int i;
10     used[num]=1;
11     for (i=1;i<=n;i++)
12     {
13         if (!used[i] && father[i]==num)
14         {
15             treee(i);
16             f[num][1]+=f[i][0];
17             f[num][0]+=max(f[i][1],f[i][0]);
18         }
19     }
20 }
21 int main()
22 {
23     int root,a,b,bb;
24     int i;
25     while (~scanf("%d",&n))
26     {
27         memset(father,0,sizeof(father));
28         memset(used,0,sizeof(used));
29         memset(f,0,sizeof(f));
30         for (i=1;i<=n;i++)
31             scanf("%d",&f[i][1]);
32         root=0;
33         bb=1;
34         while (scanf("%d%d",&a,&b),a||b)
35         {
36             father[a]=b;
37             if (a==root || bb==1)
38             {
39                 root=b;
40             }
41         }
42         while (father[root])
43             root=father[root];
44         treee(root);
45     printf("%d",max(f[root][0],f[root][1]));
46     }
47     return 0;
48 }
时间: 2024-11-06 05:11:23

【Poj】 p2342 Anniversary party(树形DP第一道)的相关文章

poj 2324 Anniversary party(树形DP)

/*poj 2324 Anniversary party(树形DP) ---用dp[i][1]表示以i为根的子树节点i要去的最大欢乐值,用dp[i][0]表示以i为根节点的子树i不去时的最大欢乐值, ---于是当i去时,i的所有儿子都不能去:dp[i][1]=sum(dp[j][0])+a[i],其中j是i的儿子节点. ---当i不去时,i的儿子可去也可不去:dp[i][0]=sum(max(dp[j][0],dp[j][1])),j是i的儿子节点 ---边界条件:当i时叶子节点时,dp[i][

POJ 2342 Anniversary party (树形dp 入门题)

Anniversary party Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4810   Accepted: 2724 Description There is going to be a party to celebrate the 80-th Anniversary of the Ural State University. The University has a hierarchical structure

POJ 2342 Anniversary party 树形dp入门

http://poj.org/problem?id=2342 某公司要举办一次晚会,但是为了使得晚会的气氛更加活跃,每个参加晚会的人都不希望在晚会中见到他的直接上司,现在已知每个人的活跃指数和上司关系(当然不可能存在环),求邀请哪些人(多少人)来能使得晚会的总活跃指数最大. 对于每一个人,都有两种情况,选,或者不选.然后选了后,他的下属就只能不选了,如果它不选,下属可能选或者不选. 这样用dfs + 记忆化搜索,就好了 每次搜索,就是要求以第cur个节点为根的子树中,其中第cur个节点的状态是s

POJ 2342 Anniversary party 树形DP基础题

题目链接:http://poj.org/problem?id=2342 题目大意:在一个公司中,每个职员有一个快乐值ai,现在要开一个party,邀请了一个员工就不可能邀请其直属上司,同理邀请了一个人就不可以邀请其的直属员工, 问如何使得这个快乐值达到最大. 题解:对每个结点dp[i][0]表示不邀请这个员工,其子树达到的最大快乐值,dp[i][1]表示邀请i员工其子树达到的最大值. dp[i][0]=(i的全部员工的max(dp[u][1],dp[u][0)相加,也就是其子员工来或不来的最大快

POJ 2342 Anniversary Party ( 树形DP )

deque 的插入操作不一定有 vector 快 #include <iostream> #include <cstring> #include <fstream> #include <vector> using namespace std; #define NOT_SELECTED 0 #define SELECTED 1 #define SIZE 6001 vector< int > relations[SIZE]; bool visited

hdu 1520 Anniversary party 生日party 树形dp第一题

Anniversary party Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 8667    Accepted Submission(s): 3748 Problem Description There is going to be a party to celebrate the 80-th Anniversary of the

POJ 2342 &amp;&amp;HDU 1520 Anniversary party 树形DP 水题

一个公司的职员是分级制度的,所有员工刚好是一个树形结构,现在公司要举办一个聚会,邀请部分职员来参加. 要求: 1.为了聚会有趣,若邀请了一个职员,则该职员的直接上级(即父节点)和直接下级(即儿子节点)都不能被邀请 2.每一个员工都有一个兴奋值,在满足1的条件下,要使得邀请来的员工的兴奋值最高 输出最高的兴奋值. 简单的树形DP dp[i][1]:表示以i为根的子树,邀请节点i的最大兴奋值 dp[i][0]:表示以i为根的子树,不邀请节点i的最大兴奋值 先根据入度找出整棵树的根节点, 然后一次DF

POJ Anniversary party 树形DP

/* 树形dp: 给一颗树,要求一组节点,节点之间没有父子关系,并且使得所有的节点的权值和最大 对于每一个节点,我们有两种状态 dp[i][0]表示不选择节点i,以节点i为根的子树所能形成的节点集所能获得的最大权值和 dp[i][1]表示选择节点i ,同上! 转移方程: dp[i][0]+=max(dp[i_son][1],dp[i_son][0])如果没选择的话,那么子树可选择可不选择 dp[i][1]+=dp[i_son][0] 选择了之后,子树只能不选择 最后输出max(dp[i][0],

HDU-1520 Anniversary party (树形DP)

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

POJ 2342 Anniversary party (树dp)

题目链接:http://poj.org/problem?id=2342 有n个人,每个人有活跃值.下面n-1行u和v表示u的上司是v,有直接上司和下属的关系不能同时参加party,问你party最大的活跃值是多少. 也就是说一棵树中,选择的点不能是相邻的点,且要使活跃值最大. 简单的树形dp,任意选一个点开始遍历,从叶子节点开始回溯. dp[i][0]表示不选i节点最大的活跃度,则dp[i][1]表示选i节点最大的活跃度. i与j相连,dp[i][0] += max(dp[j][0], dp[j