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);
	}
}

 

全部评论

相关推荐

刚刷到字节跳动官方发的消息,确实被这波阵仗吓了一跳。在大家还在纠结今年行情是不是又“寒冬”的时候,字节直接甩出了史上规模最大的转正实习计划——ByteIntern。咱们直接看几个最硬的数,别被花里胡哨的宣传词绕晕了。首先是“量大”。全球招7000多人是什么概念?这几乎是把很多中型互联网公司的总人数都给招进来了。最关键的是,这次的资源分配非常精准:研发岗给了4800多个Offer,占比直接超过六成。说白了,字节今年还是要死磕技术,尤其是产品和AI领域,这对于咱们写代码的同学来说,绝对是今年最厚的一块肥肉。其次是大家最关心的“转正率”。官方直接白纸黑字写了:整体转正率超过50%。这意味着只要你进去了,不划水、正常干,每两个人里就有一个能直接拿校招Offer。对于2027届(2026年9月到2027年8月毕业)的同学来说,这不仅是实习,这简直就是通往大厂的快捷通道。不过,我也得泼盆冷水。坑位多,不代表门槛低。字节的实习面试出了名的爱考算法和工程实操,尤其是今年重点倾斜AI方向,如果你简历里有和AI相关的项目,优势还是有的。而且,转正率50%也意味着剩下那50%的人是陪跑的,进去之后的考核压力肯定不小。一句话总结:&nbsp;27届的兄弟们,别犹豫了。今年字节这是铁了心要抢提前批的人才,现在投递就是占坑。与其等到明年秋招去千军万马挤独木桥,不如现在进去先占个工位,把转正名额攥在手里。
喵_coding:别逗了 50%转正率 仔细想想 就是转正与不转正
字节7000实习来了,你...
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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