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. SqlServer-- NULL空值处理

    数据库中,一个列如果没有指定值,那么值就为null,数据库中的null表示"不知道",而不是表示没有.因此select null+1结果是null,因为"不知道" ...

  2. VS2015 Git使用教程——优化项目编辑日志

    一.前言 公司项目中,修改日志管理是一件很繁琐的事情,而且项目维护时间长了,会遗留下各种有用或无用的日志,对于有代码洁癖的人来说,无疑是一种灾难. 1.公司日志记录结构: 2.Git日志记录结构: 二 ...

  3. 烂泥:Postfix邮件服务器搭建之软件安装与配置

    本文由ilanniweb提供友情赞助,首发于烂泥行天下 想要获得更多的文章,可以关注我的微信ilanniweb Postfix邮件服务器的搭建需要使用到几个软件,分别是cyrus-sasl.postf ...

  4. Tips for Planning Your Business Startup

    原文链接:http://domaintree.me/?p=1037 By Robert Thibodeau –  Starting a business can be a very daunting ...

  5. 利用varnish做Discuz论坛的缓存服务器

    实验背景:公司有一台BBS服务器,用的是LNMP的架构搭建的.正好手头有一台空闲的虚拟机,于是想着给BBS前端加一台缓存服务器.于是选定了varnish,搜了很多教程,跌跌撞撞的完成了配置.这其中很多 ...

  6. 修改 EF的默认连接工厂为 Sql Server 而不是LocalDb

      当你用EF6创建一个新项目,不知你是否注意到默认的连接字符串使用了LocalDb而不是SQLServer.但你如果想把默认连接改用SQLSErver而不是LocalDb.这个其实很简单:只需修改下 ...

  7. 关于Web报表FineReport打印的开发应用案例

    报表打印是报表使用和开发过程中经常碰到的问题,这里汇总了关于Web报表开发打印功能的一些典型应用案例,以应用最广泛的FineReport为例. 案例一:java直接调用报表打印 当java后台定义定时 ...

  8. Neutron 理解 (2): 使用 Open vSwitch + VLAN 组网 [Netruon Open vSwitch + VLAN Virutal Network]

    学习 Neutron 系列文章: (1)Neutron 所实现的虚拟化网络 (2)Neutron OpenvSwitch + VLAN 虚拟网络 (3)Neutron OpenvSwitch + GR ...

  9. .Net 扩展方法集合.

      在项目中很多时候都会对字符串和集合做特定的处理.而且很多地方都会去调用.为了解决这些问题.我们通常会在项目中吧这些方法提成公共方法.下面是自己总结的项目中用到的一些扩展方法.封装成了一个Libra ...

  10. Map接口使用注意事项

    1,Map接口对象本身不能直接使用迭代进行输出的.因为map每个位置存放的是一对值. 而iterator每次只能找到一个值.如果一定要迭代输出,可以通过以下步骤.: 但是,Map接口只作为查找使用,输 ...