HDU1520_Anniversary party_树状DP

Anniversary party

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


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

大致题意:

公司召开联欢会,希望场面热闹一点。每一位员工都有一个活跃值,总的活跃值越大,联欢会越热闹。但是有一个要求,员工不能和他的直接上司同时参见。请设计最佳方案使活跃值最大。


大体思路:

树状DP。

对于每个人有两种选择:1)请他,用1表示。2)不请他,用0表示。由此写出状态转移方程:

dp[0][i] = max ( dp[1][i+1] , dp[0][i+1] );

dp[1][i] = dp[0][i+1];


#include<cstdio>
#include<cstring>

#define max(x,y) x>y ? x : y

/*******链式前向星的零件*******/
/****头指针表、链接表、对应的对象、链接表的大小****/
int Head [6010], Seq [6010], No [6010], len;//在这个题的条件下No数组和tot实际可以不用
///树形DP的关键
///0表示不带这个人,1表示带这个人
int DP [2][6010];
bool Lower [6010];//值为“真”表示这个人当过下属
int N;

/*******链式前向星插入函数*******/
void Insert (int down, int up)
{
	No[len] = down;//记录下当前位置对应的对象
	Seq[len] = Head[up];//当前位置记录前一个小伙伴
	Head[up] = len;//上级记住当前这个人是他的下级
	len ++ ;//链接表大小加一
}

void Dfs (int r)//DP函数
{
/*******链式前向星的访问*******/
/****i初始化为头指针,每次访问当前对象前一个的小伙伴,-1为访问结束标志****/
	for(int i=Head[r]; i!=-1; i=Seq[i]){
		Dfs(No[i]);
		DP[1][r] += DP[0][No[i]];//若当前这个人参加,他的直接下级就不能参加了
		DP[0][r] += max(DP[0][No[i]], DP[1][No[i]]);//若他不参加,就加一个较大值
	}
}

int main ()
{
	while(scanf("%d",&N) != EOF){

		memset(DP,0,sizeof(DP));//DP数组归零

		for(int i=1; i<=N; i++)//直接把每个人的活跃度记录在DP数组这个人参加的位置上
			scanf("%d",&DP[1][i]);

/*******链式前向星的初始化1*******/
/*******头指针表全部初始化为-1********/
		memset(Head,-1,sizeof(Head));
		memset(Lower,0,sizeof(Lower));//开始时所有人都不是下级
		int up, down;//up是上级,down是下级
/*******链式前向星的初始化2*******/
/*******长度初始化为0*******/
		len = 0;
		while(scanf("%d%d",&down, &up) && (down+up) ){
			Lower[down] = 1;
/*******把a、b插入链式前向星*******/
			Insert(down,up);
		}
		
		int r = 1;//找到最高级领导
		while(Lower[r]) r++;

		Dfs(r);//DP

		int ans = max(DP[0][r], DP[1][r]);//最高级领导参与或不参与两情况中的最大值
		printf("%d\n",ans);

	}

	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值