您的位置:首页 > 其它

POJ 2342 Anniversary party

2015-10-29 13:11 288 查看
Anniversary party

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 5505 Accepted: 3153
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

题意:
某公司要举办一次晚会,为了使得晚会的气氛更加活跃,每个参加晚会的人都不希望见到他的直接上司,

现在已知每个人的活跃指数和上司关系(当然不可能存在环),求邀请哪些人来能使得晚会的总活跃指数最大。

分析:

树形dp,将每个人看成树中的节点,每个人有两种选择,去或不去

这里用dp[ x ][ 1 ] 表示编号为 x 的人去,并以它为根所得到的最大活跃指数

dp[ x ][ 0 ] 表示编号为 x 的人不去,并以它为根所得到的最大活跃指数

状态转移:

dp[ x ][ 1 ] += dp[ y ][ 0 ] 当 x 去的时候,它的下属y不去

dp[ x ][ 0 ] += max(dp[ y ][ 0 ], [ y ][ 1 ]) x不去的时候,它的下属有两种可能

代码:

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

const int INF = 0x7fffffff;
const int MAXN = 6000 + 10;
vector<int> V[MAXN];
int num[MAXN], dp[MAXN][2];
int f[MAXN];

int dfs(int x, int i)
{
if (dp[x][i] != INF) return dp[x][i];   //记忆化搜索
int k = V[x].size();
if (k == 0)         //当前节点没有孩子,它是叶子节点
{
dp[x][0] = 0;
dp[x][1] = num[x];
}
else
{
if (i == 1)     //编号为 x 的人去
{
dp[x][i] = num[x];      //初始化
for (int j = 0; j < k; j++)     //将孩子节点都不去的值加起来
{
int t = V[x][j];
dp[x][i] += dfs(t, 0);
}
}
else            //编号为 x 的人不去
{
dp[x][i] = 0;
for (int j = 0; j < k; j++)
{
int t = V[x][j];
dp[x][i] += max(dfs(t, 1), dfs(t, 0));  //此时它的孩子可以去或不去
}
}
}
return dp[x][i];
}
int main()
{
int n;
while (~scanf("%d", &n))
{
for (int i = 1; i <= n; i++)
{
scanf("%d", &num[i]);
V[i].clear();
f[i] = -1;
dp[i][0] = dp[i][1] = INF;
}

int a, b;
while (scanf("%d%d", &a, &b), a + b)
{
f[a] = b;      //a 的父亲为 b
V[b].push_back(a);
}
int root = 1;
while (f[root] != -1) root = f[root];   //找到根节点
int Max = max(dfs(root, 0), dfs(root, 1));
printf("%d\n", Max);
}
return 0;
}

以前的那份代码在HDU上竟然WA了,花了半天时间才发现是 dfs 最后忘了加一句 
return dp[x][i];


真是自己坑了自己,还是对递归函数的使用不太熟练。查找错误的时候在网上发现大牛的非记忆化搜索的方法

代码:

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

const int MAXN = 6000 + 10;
vector<int> V[MAXN];
int num[MAXN], dp[MAXN][2];
int f[MAXN];

void dfs(int x)
{
int len = V[x].size();
dp[x][1] = num[x];
for (int i = 0; i < len; i++)
dfs(V[x][i]);
for (int i = 0; i < len; i++)
{
int k = V[x][i];
dp[x][1] += dp[k][0];
dp[x][0] += max(dp[k][0], dp[k][1]);
}
}
int main()
{
int n;
while (~scanf("%d", &n))
{
for (int i = 1; i <= n; i++)
{
scanf("%d", &num[i]);
V[i].clear();
f[i] = -1;
dp[i][0] = dp[i][1] = 0;
}

int a, b;
while (scanf("%d%d", &a, &b), a + b)
{
f[a] = b;         //a 的父亲为 b
V[b].push_back(a);
}
int root = 1;
while (f[root] != -1) root = f[root];
dfs(root);
int Max = max(dp[root][0], dp[root][1]);
printf("%d\n", Max);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: