Anniversary party
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 7230   Accepted: 4162

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 N – 1 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

Source

 

思路

题意:某公司要举办一次晚会,但是为了使得晚会的气氛更加活跃,每个参加挽回的人都不希望见到自己的直接上司。现在已知每个人的活跃指数和上司关系(不存在环),求晚会的活跃指数的最大值。思路:dp[x][0]表示x去参加晚会,dp[x][1]表示x不去参加晚会。那么有以下两种情况:
  • x去参加晚会,则他的直接下属y就不能参加晚会,dp[ x ][ 1 ] = dp[ y [ 0 ]
  • x不去参加晚会,则他的直接下属y可以参加也可以不去参加,dp[ x ][ 0 ] = max ( dp[ y ][ 0 ],dp[ y ][ 1 ])
/*
	dp[x][0]:x 不去参加聚会
	dp[x][1]:x 去参加聚会
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn =  6005;
int tot = 0,fa[maxn],head[maxn],dp[maxn][2];;
struct Tree{
	int fa,son;
	int next;
	Tree():fa(0),son(0),next(0){}
}tree[maxn];

void addedge(int u,int v)
{
	tree[tot].son = v;
	tree[tot].next = head[u];
	head[u] = tot++;
}

void dfs(int cur)
{
	for (int i = head[cur];i != -1;i = tree[i].next)
	{
		dfs(tree[i].son);
		dp[cur][1] += dp[tree[i].son][0];
		dp[cur][0] += max(dp[tree[i].son][0],dp[tree[i].son][1]);
	}
}

int main()
{
	int N;
	while (~scanf("%d",&N))
	{
		memset(head,-1,sizeof(head));
		memset(dp,0,sizeof(dp));
		for (int i = 1;i <= N;i++)	scanf("%d",&dp[i][1]);
		int L,K;
		while (scanf("%d%d",&L,&K) && L && K)
		{
			tree[L].fa = K;
			addedge(K,L);
		}
		int root = 1;
		while (tree[root].fa)	root = tree[root].fa;
		dfs(root);
		printf("%d\n",max(dp[root][0],dp[root][1]));
	}
	return 0;
}

  

  

POJ 2342 Anniversary party(树形dp)的更多相关文章

  1. POJ 2342 - Anniversary party - [树形DP]

    题目链接:http://poj.org/problem?id=2342 Description There is going to be a party to celebrate the 80-th ...

  2. poj 2342 Anniversary party 树形DP入门

    题目链接:http://poj.org/problem?id=2342 题意:一家公司有1 <= N <= 6 000个职工,现要组织一些职工参加晚会,要求每个职工和其顶头上司不能同时参加 ...

  3. POJ 2342 Anniversary party 树形DP基础题

    题目链接:http://poj.org/problem?id=2342 题目大意:在一个公司中,每个职员有一个快乐值ai,现在要开一个party,邀请了一个员工就不可能邀请其直属上司,同理邀请了一个人 ...

  4. poj 2324 Anniversary party(树形DP)

    /*poj 2324 Anniversary party(树形DP) ---用dp[i][1]表示以i为根的子树节点i要去的最大欢乐值,用dp[i][0]表示以i为根节点的子树i不去时的最大欢乐值, ...

  5. POJ 2342 Anniversary party (树dp)

    题目链接:http://poj.org/problem?id=2342 有n个人,每个人有活跃值.下面n-1行u和v表示u的上司是v,有直接上司和下属的关系不能同时参加party,问你party最大的 ...

  6. poj 2342 && hdu 1520 树形dp

    题意:有n个人,接下来n行是n个人的价值,再接下来n行给出l,k说的是l的上司是k,这里注意l与k是不能同时出现的 链接:点我 dp[i][1] += dp[j][0], dp[i][0] += ma ...

  7. DP Intro - poj 2342 Anniversary party

    今天开始做老师给的专辑,打开DP专辑 A题 Rebuilding Roads 直接不会了,发现是树形DP,百度了下了该题,看了老半天看不懂,想死的冲动都有了~~~~ 最后百度了下,树形DP入门,找到了 ...

  8. POJ 2342 Anniversary party / HDU 1520 Anniversary party / URAL 1039 Anniversary party(树型动态规划)

    POJ 2342 Anniversary party / HDU 1520 Anniversary party / URAL 1039 Anniversary party(树型动态规划) Descri ...

  9. POJ 2342 &&HDU 1520 Anniversary party 树形DP 水题

    一个公司的职员是分级制度的,所有员工刚好是一个树形结构,现在公司要举办一个聚会,邀请部分职员来参加. 要求: 1.为了聚会有趣,若邀请了一个职员,则该职员的直接上级(即父节点)和直接下级(即儿子节点) ...

  10. poj 2342 Anniversary party 简单树形dp

    Anniversary party Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3862   Accepted: 2171 ...

随机推荐

  1. Android Fragment生命周期

    Fragment与Activity的生命周期关系: 刚打开Activity:Fragment onAttach > Fragment onCreate > Fragment onCreat ...

  2. CentOS下配置nginx conf/koi-win为同一文件的各类错误

    今天配置CentOS6.5下安装Nginx + php7 + mysql5.7.15遇到了一些坑.本来家里的电脑在配置环境的时候没有问题,拿去公司的电脑上就是到处报错.不知道是不是人品问题.今晚在家重 ...

  3. MySQL server has gone away报错原因分析/

    在平时和开发的交流 以及 在论坛回答问题的或称中会发现这个问题被问及的频率非常高. 程序中报错: MySQL server has gone away 是什么意思? 如何避免? 因此,感觉有必要总结一 ...

  4. php使用curl 检测socks5 代理的可用性

    少废话  直接粘代码 <?php    define('PROXY_CONF', 'ip:port');    define('PROXY_CHECK_URL', 'http://www.bai ...

  5. python_面向对象编程

    一.编程范式 程序员通过特定的语法+数据结构+算法告诉计算机如果执行任务,实现这个过程有不同的编程方式,对这些不同的编程方式进行归纳总结得出来的编程方式类别,即为编程范式 编程范式:面向过程编程.面向 ...

  6. centos 7 安装音乐播放器(亲测可用)(转载)

    http://www.cnblogs.com/boyiliushui/p/4530625.html

  7. U3D笔记11:47 2016/11/30-15:15 2016/12/19

    11:47 2016/11/30Before you can load a level you have to add it to the list of levels used in the gam ...

  8. MYSQL的卸载

    卸载mysql 1.查找以前是否装有mysql 命令:rpm -qa|grep -i mysql 可以看到mysql的两个包: mysql-4.1.12-3.RHEL4.1 mysqlclient10 ...

  9. MySql操作

    用C#写了个系统更新的小程序,用到MySql MySQL 5.1参考手册 1.使用MySqlConnection 需要引用dll: MySql.Data.rar 连接数据库 string conStr ...

  10. Swift学习(一):自定义运算符 operator

    自定义运算符仅能包含这些字符: / = - + * % < >!& | ^.~ 运算符位置: 前置运算符 prefix 中间运算符 infix 后置运算符 postfix 运算符其 ...