HDU - 1520 Anniversary party (树形dp)

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

 

   上级下级不能同时出现,每个人出现有着自己独特的值,问让出现的人的值综合最大是多少。

   这道题一看到上级下级,就会有人要建立有向图,其实不然,上级下级之间有的只是不能共存着一个特点而已,并没有像树一样需要你严格从上往下遍历,所以建立无向图之后随便找一个作为根遍历就好,注意加一个vis数组防止存在环。

#include<bits/stdc++.h>
using namespace std;
const int maxn = 20000;
struct fcuk {
	int u, v, ne;
}ed[maxn];
int n, head[maxn], cnt;
int v[maxn], dp[maxn][3], vis[maxn];
void init() {
	memset(dp, 0, sizeof(dp));
	memset(head, -1, sizeof(head));
	memset(vis, 0, sizeof(vis));
	cnt = 0;
}
void add(int u, int v) {
	ed[cnt].u = u; ed[cnt].v = v;
	ed[cnt].ne = head[u]; head[u] = cnt++;
}
void dfs(int x) {
	vis[x] = 1;
	dp[x][1] = v[x];
	for (int s = head[x]; ~s; s = ed[s].ne) {
		int v = ed[s].v;
		if (vis[v])continue;
		dfs(v);
		dp[x][0] += max(dp[v][1], dp[v][0]);
		dp[x][1] += dp[v][0];
	}
}
int main() {
	while (~scanf("%d", &n)) {
		init();
		for (int s = 1; s <= n; s++){
			scanf("%d", &v[s]);
		}
		int a, b;
		while (~scanf("%d%d", &a, &b) && a + b) {
			add(a, b); add(b, a);
		}
		dfs(1);
		int ans = max(dp[1][0], dp[1][1]);
		printf("%d\n", ans);
	}
}

 

全部评论

相关推荐

就前几天旅游的时候,打开抖音就经常刷到这类视频:以前是高学历学生、老师、主持人,现在做着团播、擦边主播的工作,以及那些经过精心包装的“职业转型”故事——从铺天盖地的VLOG到所谓的“04年夜场工作日记”,这些内容在初中升学、高考放榜等关键时间节点持续发酵。可以说非常直接且精准地在潜移默化地影响着心智尚未成熟的青少年,使其对特殊行业逐渐脱敏。那我就想问了:某些传播公司、平台运营者甚至某些夜场的老板,你们究竟在传递怎样的价值观?点开那些视频,评论区里也是呈现明显的两极分化:一种是​​经济下行论​​:“现在就业市场已经艰难到这种程度了吗?”​​一种是事实反驳派​​:这些创作者往往拥有名校背景,从事着...
牛客刘北:被环境教育的,为了能拿到足够的钱养活自己,不甘心也得甘心,现在的短视频传播的思想的确很扭曲,但是很明显,互联网玩上一年你就能全款提A6,但你全心全意不吃不喝工作一年未必能提A6,但是在高考中考出现这个的确很扭曲,在向大家传播“不上学,玩互联网也可以轻松年入百万”,不是人变了,是社会在变
预测一下26届秋招形势
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-04 18:25
点赞 评论 收藏
分享
程序员饺子:正常 我沟通了200多个 15个要简历 面试2个 全投的成都的小厂。很多看我是27直接不会了😅
点赞 评论 收藏
分享
牛客92804383...:在他心里你已经是他的员工了
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-03 17:37
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务