POJ 2342 Anniversary party(树形dp)

时间:2022-09-02 11:33:41
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 - &lbrack;树形DP&rsqb;

    题目链接: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&lpar;树形DP&rpar;

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

  5. POJ 2342 Anniversary party &lpar;树dp&rpar;

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

  6. poj 2342 &amp&semi;&amp&semi; 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&&num;160&semi;Anniversary party

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

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

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

  9. POJ 2342 &amp&semi;&amp&semi;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. 使用Go开发HTTP中间件

    原文地址    再web开发的背景下,"中间件"通常意思是"包装原始应用并添加一些额外的功能的应用的一部分".这个概念似乎总是不被人理解,但是我认为中间件非常棒 ...

  2. iOS—最全的真机测试教程

    准备 开发者账号 自从Xcode7 出来之后,一般的真机测试不需要开发者账号,也就不需要看这篇教程,只有app具有“推送”等功能的时候,要真机测试就必须要开发者账号和设置证书.苹果只是让你体验一下它的 ...

  3. 用PHP的socket实现客户端到服务端的通信

    服务端 <?php error_reporting(E_ALL); set_time_limit(0); ob_implicit_flush(); //本地IP $address = 'loca ...

  4. Java迷题:等于,还是不等于?

    等于还是不等于? 看来看下面的一段代码: public static void main(final String[] args) { Integer a = new Integer(100); In ...

  5. 对require&period;js 的使用进行总结

    一.为什么要使用require.js 首先一个页面如果在加载多个js文件的时候,浏览器会停止网页渲染,加载文件越多,网页失去响应的时间就会越长:其次,由于js文件之间存在依赖关系,因此必须严格保证加载 ...

  6. mysql时间int日期转换

    select from_unixtime(1350437720);select unix_timestamp(now());插入用 unix_timestamp(date)查询用from_unixti ...

  7. BZOJ 2730&colon; &lbrack;HNOI2012&rsqb;矿场搭建&lpar; tarjan &rpar;

    先tarjan求出割点.. 割点把图分成了几个双连通分量..只需dfs找出即可. 然后一个bcc有>2个割点, 那么这个bcc就不用建了, 因为一定可以走到其他救援出口. 只有一个割点的bcc就 ...

  8. Tomcat 部署安装及JVM调优~

    Tomcat 部署Tomcat环境 环境准备 linux: CentOS 7.3 tomcat: 9.0.0.M21 jdk: 1.8.0_131 ip: 192.168.1.5 tomcat官方下载 ...

  9. JavaSE:八种基本数据类型

    变量: 程序用来存储数据的一块内存空间,程序在运行过程中可以对其存储的数据进行改变,所以叫做变量 常量:相对于变量来说,其值是不可改变的 ​ 整数类型(byte short int long) ​ b ...

  10. 万维网WWW详解

    万维网WWW(World Wide Web)并非某种特殊的计算机网络,万维网是一个个大规模的.联机式的信息储藏所,英文简称Web. 万维网使用链接的方式能非常方便地从英特网上的一个站点访问到一个站点, ...