HDU 1520 树形dp

Anniversary party

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 12941    Accepted Submission(s): 5194


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. 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 T 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
题意:公司要开派对 ,每个人都有自己的 派对兴奋值 。 但是兴奋值是建立在没有遇见 直系长官的情况下,,问你 派对的最大兴奋值。 关系以 0 0结束。
思路:很简单的树形dp 树形dp 一般有 从节点到父节点,和从父节点到子节点
对于 是从 子节点到父节点, 对于每一个人 有来和不来两种情况。
分别设为 dp[fa][0] dp[fa][1] 现在思考 , 对于一个节点 如果这个人
来参加聚会 那么他的直接儿子 就不能来参加 派对。
所以有 dp[fa][1]+=dp[erzi][0];
对于每一个人 不来的话,, 那么他的直接儿子来不来是不能确定的,我们要就看他的直接儿子 来 还是不来 的 哪个兴奋值最大。
所以有:dp[fa][0]+=max(dp[erzi][0],dp[erzi][1]);
代码1:
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#define N 6005
#define M 6005

using namespace std;

struct node
{
	int u,v,next;
}edge[M];

int head[N],inde[N],out[N];
int n,m,cnt;
int act[N];
int dp[N][2];

void solve(int fa)
{
	if(out[fa]==0)
	{
		dp[fa][0]=0;
		dp[fa][1]=act[fa];
		return ;
	}
	
	dp[fa][0]=0;
	dp[fa][1]=act[fa];
	
	int suma=0;
	int sumb=0;
	
	for(int k=head[fa];k!=-1;k=edge[k].next)
	{
		int v=edge[k].v;
		solve(v);
		dp[fa][1]+=dp[v][0];
		dp[fa][0]+=max(dp[v][0],dp[v][1]);
	}
	
	//printf("fa:  %d  %d\n",fa,max(dp[fa][0],dp[fa][1]));
	return ;
}

void add(int u,int v)
{
	edge[++cnt].u=u;
	edge[cnt].v=v;
	edge[cnt].next=head[u];
	head[u]=cnt;
	return ;
}

int main()
{
	int i,u,v;
	while(scanf("%d",&n)!=EOF)
	{
		memset(inde,0,sizeof(inde));
	memset(out,0,sizeof(out));
	memset(head,-1,sizeof(head));
	cnt=0;
	memset(dp,0,sizeof(dp));
	
	for(i=1;i<=n;i++) scanf("%d",&act[i]);
	
	while(scanf("%d %d",&u,&v)!=EOF){
		if(u==0&&v==0) break;
		add(v,u);
		inde[u]++;
		out[v]++;
	}
	
	for(i=1;i<=n;i++)
	{
		if(inde[i]==0)
		{
			solve(i);
			break;
		}
	}
	
	printf("%d\n",max(dp[i][0],dp[i][1]));
	
	}
	
	return 0;
	
}

代码2:

#include<stdio.h>
#include<string.h>
#include<vector>
#include<iostream>
#include<algorithm>
#define N 6005

using namespace std;

vector< int >v[N];
int inde[N];
int dp[N][2];
int n;

void solve(int fa)
{
	if(v[fa].size()==0){
		return ;
	}
	
	for(int i=0;i<v[fa].size();i++)
	{
		int erzi=v[fa][i];
		solve(erzi);
		dp[fa][1]+=dp[erzi][0];
		dp[fa][0]+=max(dp[erzi][0],dp[erzi][1]);
	}
	return ;
}

int main()
{
	int i,uu,vv;
	while(scanf("%d",&n)!=EOF)
	{
		memset(dp,0,sizeof(dp));
		for(i=1;i<=n;i++) v[i].clear();
	for(i=1;i<=n;i++) scanf("%d",&dp[i][1]);
	
	while(scanf("%d %d",&uu,&vv)!=EOF)
	{
		if(uu==0&&vv==0) break;
		v[vv].push_back(uu);
		inde[uu]++;
	}
	
	for(i=1;i<=n;i++)
	{
		if(inde[i]==0)
		{
			solve(i);
			break;
		}
	}
	printf("%d\n",max(dp[i][0],dp[i][1]));
	
	}
	
	
	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值