POJ-2342.Anniversary party(树形DP)

博客围绕庆祝大学80周年派对嘉宾选择问题展开,要求员工和其直接主管不同时参加,需选出快乐评级和最大的嘉宾组合。采用树形DP方法,用vector数组处理树关系,定义状态dp[u][0]和dp[u][1],并给出状态转移方程求解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

树形DP

这道题大意就是参加party的人中员工和他的直接主管不能一起来,我们转化一下就是选出最多的结点其中不存在直接的父子结点。然后求出他们中快乐评级和的最大值。
在处理树方面,用vector数组来保存他们的关系。
对于每个人,都有两种状态,来或者不来:
当来时:那他的子节点都不能来;
当不来时:那他的子节点来或者不来,两种都可以
现在状态出来了,我们定义dp[u][0]表示第u个人不来,dp[u][1]表示第u个人来。
当u来时: dp[u][1]=w[u]+dp[v][0]
当u不来时:dp[u][0]+=max(dp[v][0],dp[v][1]) //子节点来或不来

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;
const int maxn = 6010;


int dp[maxn][2]; //dp[i][0]表示第i个人不来,dp[i][1]表示第i个人来
vector<int> g[maxn];
int W[maxn];

int get_dp(int x,int s,int fa) //x表示当前结点,s表示两种状态,fa表示x的根结点
{
	if(dp[x][s] != -1) 
		return dp[x][s];
	dp[x][s]=0;
	if(s) //s为1,表示来,那它的子节点不能来
	{
		dp[x][s]=W[x];
		for(int i=0;i<g[x].size();i++)
		{
			if(g[x][i]!=fa) // 注意当前结点是否是当前设置的父结点
				dp[x][s]+=get_dp(g[x][i],0,x);
		}
	}
	else
	{
		for(int i=0;i<g[x].size();i++)
		{
			if(g[x][i]!=fa) //注意当前结点是否是当前设置的父结点
				dp[x][s]+=max(get_dp(g[x][i],0,x),get_dp(g[x][i],1,x));
		}
	}
	return dp[x][s];
}
int main()
{
	int n;
	while(scanf("%d",&n)!=EOF)
	{
		memset(dp,-1,sizeof(dp));
		for(int i=1;i<=n;i++)
		{
			g[i].clear();
		}
		for(int i=1;i<=n;i++)
		{
			scanf("%d",&W[i]);
		}
		int u,v;
		while(scanf("%d%d",&u,&v)&&(u||v))
		{
			g[u].push_back(v); //把主管和员工连在一起
			g[v].push_back(u);
		}
		printf("%d\n",max(get_dp(1,0,-1),get_dp(1,1,-1)));
	}
	return 0;

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值