HDU - 1520 Anniversary party (有向入门树形DP)

题目链接:https://vjudge.net/problem/HDU-1520

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.

InputEmployees 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 0OutputOutput 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[i][0]表示节点i不选,dp[i][1]节点i选。

AC代码:

#include<stdio.h>
#include<vector>
#include<string.h>
#include<algorithm>
using namespace std;
const int maxn=6010;
vector<int>map[maxn];
int dp[maxn][2],a[maxn],N,vis[maxn];
void dfs(int u){
	 dp[u][0]=0;
	 dp[u][1]=a[u];
	for(int i=0;i<map[u].size();i++)
	{
		int v=map[u][i];
		dfs(v);
		dp[u][0]+=max(dp[v][0],dp[v][1]);
		dp[u][1]+=dp[v][0];
	}
}
int main(){
	while(~scanf("%d",&N)){
		for(int i=1;i<=N;i++) scanf("%d",&a[i]);
		for(int i=1;i<=N;i++) map[i].clear();
		memset(vis,0,sizeof(vis));
		int u,v;
		while(scanf("%d%d",&v,&u)&&u&&v)
		{
			map[u].push_back(v);
			vis[v]++;
		}
		for(int i=1;i<=N;i++)
			if(!vis[i]){
				dfs(i);
				printf("%d\n",max(dp[i][0],dp[i][1]));
				break;
			}
	}
	return 0;
}

  

原文地址:https://www.cnblogs.com/djh0709/p/9606477.html

时间: 2024-10-28 15:45:49

HDU - 1520 Anniversary party (有向入门树形DP)的相关文章

hdu 1520 Anniversary party(第一道树形dp)

传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1520 Anniversary party Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 16376    Accepted Submission(s): 6241 Problem Description There is going to

HDU 1520 Anniversary party(DFS或树形DP)

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.

HDU 1520 Anniversary party 树DP水题

非常水的树DP,状态为当前为i,上级来没来 然后跑一遍记忆化搜索即可 #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <climits> #include <string> #include <iostream> #include <map> #include <cstdlib>

HDU 1520.Anniversary party 基础的树形dp

Anniversary party Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 12770    Accepted Submission(s): 5142 Problem Description There is going to be a party to celebrate the 80-th Anniversary of the

POJ 2342 &amp;&amp;HDU 1520 Anniversary party 树形DP 水题

一个公司的职员是分级制度的,所有员工刚好是一个树形结构,现在公司要举办一个聚会,邀请部分职员来参加. 要求: 1.为了聚会有趣,若邀请了一个职员,则该职员的直接上级(即父节点)和直接下级(即儿子节点)都不能被邀请 2.每一个员工都有一个兴奋值,在满足1的条件下,要使得邀请来的员工的兴奋值最高 输出最高的兴奋值. 简单的树形DP dp[i][1]:表示以i为根的子树,邀请节点i的最大兴奋值 dp[i][0]:表示以i为根的子树,不邀请节点i的最大兴奋值 先根据入度找出整棵树的根节点, 然后一次DF

hdu 1520 Anniversary party 生日party 树形dp第一题

Anniversary party Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 8667    Accepted Submission(s): 3748 Problem Description There is going to be a party to celebrate the 80-th Anniversary of the

HDU 1520 Anniversary party (树形DP)

树形DP的关键在于如何处理递归返回的信息.这题dp[i][0]表示不选i点时当前最高权值.dp[i][1]表示选i点时当前最高权值.状态转移方程:dp[u][[0]+=max(dp[v][0],dp[v][1]),dp[u][1]+=dp[v][0]; 代码如下: #include <iostream> #include <string.h> #include <math.h> #include <queue> #include <algorithm&

HDU 1520 Anniversary party

http://acm.hdu.edu.cn/showproblem.php?pid=1520 题意: 将有一个党庆祝乌拉尔国立大学80周年.大学有一个员工的层次结构.这意味着监督关系形成了一个树根源于校长VE Tretyakov.为了使党对每个人都有趣,主任不想让一个雇员和他或她的直接主管在场.人事局已评估每个员工的欢乐性,所以每个人都有一定数量(评级)附加到他或她.你的任务是制作一个客人的列表,其中有最大可能的客人的欢乐度评分的总和. 思路: 这道题目就是UVa 1220的减弱版,比较基本的树

HDU 1561 The more, The Better(树形dp+背包)

Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 6000    Accepted Submission(s): 3548 Problem Description ACboy很喜欢玩一种战略游戏,在一个地图上,有N座城堡,每座城堡都有一定的宝物,在每次游戏中ACboy允许攻克M个城堡并获得里面的宝物.但由于地理位置原因,有些城堡不能直接攻