A - Anniversary party(树形dp)

本文介绍了一种用于大学80周年庆典的嘉宾名单优化算法,通过构建员工关系树并评估每位员工的社交评分,算法旨在创建一份总社交评分最高的嘉宾名单,同时遵循不让员工及其直接上级同时出席的规则。

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

There are several cases. In each case, 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

需要注意是多组输入,如果不加最外面的while会WA。
每次开始前要把边数和数组清零,否则会溢出,导致Runtime Error(ACCESS_VIOLATION)。

f[i][0]f[i][0]f[i][0]:表示结点iii不参加舞会时的最大值

f[i][1]f[i][1]f[i][1]:表示结点iii参加舞会时的最大值

若以iii为根时,ans=max(f[i][0],f[i][1])ans=max(f[i][0],f[i][1])ans=max(f[i][0],f[i][1])

状态转移方程为:

f[i][0]=∑max(f[x][0],f[x][1])f[i][0]=\sum max(f[x][0],f[x][1])f[i][0]=max(f[x][0],f[x][1]) (iii不参加舞会时,下属随意)

f[i][1]=∑f[x][0]+aif[i][1]=\sum f[x][0]+a_if[i][1]=f[x][0]+aiiii参加舞会时,下属不参加)

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 6e3 + 5; 

struct edge {//自己和兄弟结点间的边 
	int v, next;//next表示兄弟结点 
} e[N];

int head[N], n, cnt;//head表示第一个子结点,n表示结点数, 
int is_h[N], vis[N];//表示是否是子结点,是否访问过 
int f[N][2];

void addedge(int u, int v) {//建边 
	e[++cnt].v = v;
	e[cnt].next = head[u];//让v指向它的兄弟结点 
	head[u] = cnt;//使v成为u的第一个子节点 
}

void calc(int k) {
	vis[k] = 1;
	for (int i = head[k]; i; i = e[i].next)  //枚举该节点的每个子节点
	{
		if (vis[e[i].v])//被访问过 
			continue;
		calc(e[i].v);//递归 
		f[k][1] += f[e[i].v][0];//上司参加,下属只能不参加 
		f[k][0] += max(f[e[i].v][0], f[e[i].v][1]);//上司不参加,下属随意 
	}
	return;
}

int main(void) 
{
	int n;
	while (scanf("%d", &n) != EOF){
		cnt = 0;
		memset(head, 0, sizeof head);
		memset(is_h, 0, sizeof is_h);
		memset(vis, 0, sizeof vis);
		memset(f, 0, sizeof f);
		for (int i = 1; i <= n; i++) 
			scanf("%d", &f[i][1]);
		int l, k;
		while (scanf("%d%d", &l, &k) && l && k) {
			is_h[l] = 1;
			addedge(k, l);
		}
		for (int i = 1; i <= n; i++)
			if (!is_h[i]){  //从根节点开始DFS
				calc(i);
				printf("%d\n", max(f[i][1], f[i][0]));
				break;
			}
	}
	return 0;
}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值