poj2342 Anniversary party (树形dp)

poj2342 Anniversary party (树形dp)

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 9128   Accepted: 5250

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

Ural State University Internal Contest October‘2000 Students Session

大致题意:

某公司要举办一次晚会,但是为了使得晚会的气氛更加活跃,每个参加晚会的人都不希望在晚会中见到他的直接上司,现在已知每个人的活跃指数和上司关系(当然不可能存在环),求邀请哪些人(多少人)来能使得晚会的总活跃指数最大。

思路:

思路:

任何一个点的取舍可以看作一种决策,那么状态就是在某个点取的时候或者不取的时候,以他为根的子树能有的最大活跃总值。分别可以用f[i,1]和f[i,0]表示第i个人来和不来。

上司来,下属不来

当i来的时候,dp[i][1] += dp[j][0];//j为i的下属

上司不来,下属来或不来

当i不来的时候,dp[i][0] +=max(dp[j][1],dp[j][0]);//j为i的下属

 1 #include<iostream>
 2 #include<cmath>
 3 #include<algorithm>
 4 #include<vector>
 5 #include<cstdio>
 6 #include<cstdlib>
 7 #include<cstring>
 8 #include<string>
 9
10 using namespace std;
11
12 #define maxn 6005
13
14 int n;
15 int dp[maxn][2],father[maxn];//dp[i][0]0表示不去,dp[i][1]1表示去了
16 bool visited[maxn];
17
18 void tree_dp(int node)
19 {
20     int i;
21     visited[node] = 1;
22     for(i=1; i<=n; i++) //找1到n中找node节点的下属
23     {
24         if(!visited[i]&&father[i] == node)//i为下属
25         {
26             tree_dp(i);//递归调用孩子结点,从叶子结点开始dp
27             //关键
28             dp[node][1] += dp[i][0];//上司来,下属不来
29             dp[node][0] +=max(dp[i][1],dp[i][0]);//上司不来,下属来、不来
30         }
31     }
32 }
33
34 int main()
35 {
36     int i;
37     int f,c,root;
38     while(scanf("%d",&n)!=EOF)
39     {
40         memset(dp,0,sizeof(dp));
41         memset(father,0,sizeof(father));
42         memset(visited,0,sizeof(visited));
43         for(i=1; i<=n; i++)
44         {
45             scanf("%d",&dp[i][1]);
46         }
47         root = 0;//记录父结点
48         bool beg = 1;
49         while (scanf("%d %d",&c,&f),c||f)
50         {
51             //c的老爸是f
52             father[c] = f;
53             //儿子是跟节点   或者这条边是第一条边
54             //父亲做根节点
55             if( root == c || beg )
56             {
57                 root = f;
58             }
59         }
60         //循环寻找根节点
61         while(father[root])//查找父结点
62             root=father[root];
63         //从根节点开始树形dp
64         tree_dp(root);
65         //取最大的上司去或者不去中的大值
66         int imax=max(dp[root][0],dp[root][1]);
67         printf("%d\n",imax);
68     }
69     return 0;
70
71 }  
时间: 2024-08-10 17:02:12

poj2342 Anniversary party (树形dp)的相关文章

[poj2342]Anniversary party_树形dp

Anniversary party poj-2342 题目大意:没有上司的舞会原题. 注释:n<=6000,-127<=val<=128. 想法:其实就是最大点独立集.我们介绍树形dp 树形dp就是以节点或者及其子树为信息,进行动态规划.用dfs的原理,遍历,在回溯是更新父亲节点. 然后,关于这道题,我们就可以对于每一个节点进行标记,然后对于满足条件的节点进行遍历.设状态就是两个dp,分别表示选当前根节点和不选当前根节点.更新是瞎jb更新即可... .... 最后,附上丑陋的代码...

[poj2342]Anniversary party树形dp入门

题意:选出不含直接上下司关系的最大价值. 解题关键:树形dp入门题,注意怎么找出根节点,运用了并查集的思想. 转移方程:dp[i][1]+=dp[j][0];/i是j的子树 dp[i][0]+=max(dp[j][0],dp[j][1]); 1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #include<cstdlib> 5 #include<iostream> 6

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][

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.

URAL 1039 Anniversary Party 树形DP 水题

1039. Anniversary Party Time limit: 0.5 secondMemory limit: 8 MB Background The president of the Ural State University is going to make an 80'th Anniversary party. The university has a hierarchical structure of employees; that is, the supervisor rela

HDU 1520 Anniversary party (树形DP)

树形DP的关键在于如何处理递归返回的信息.这题dp[i][0]表示不选i点时当前最高权值.dp[i][1]表示选i点时当前最高权值.状态转移方程:dp[u][[0]+=max(dp[v][0],dp[v][1]),dp[u][1]+=dp[v][0]; 代码如下: #include <iostream> #include <string.h> #include <math.h> #include <queue> #include <algorithm&

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],

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

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

HDU 1520:Anniversary party 树形DP基础

Anniversary party 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1520 题意: 公司里有一堆人再开派对,每个人有一个欢乐值,这些人之间有一些上下级关系,每个人都不想和他的直接上司(即是父亲,不包括祖先)同时出现在party上,求party上最大的欢乐值的合. 题解: 由题意知公司成员之间可以组成一棵关系树,设value[x]为以x为根节点的子树的最大权值合,child[x]为x的子节点,从树上可以看出,value[x]=max

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