Anniversary party
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 8028   Accepted: 4594

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的题、、、
思路还是比较好理解的。
每个人有来与不来两种状态,当来的时候,他的直接下属就不能来。----①
当他不来的时候,他的直接下属可以来,可以不来。----②
用dp[i][1]表示编号为i的人的时候,以它为根的子树所以产生的活跃值:由①可知dp[i][1]=dp[i][1]+dp[儿子节点][0];
用dp[i][0]表示编号为i的人不来的时候,以它为根的子树所产生的活跃值:由②可知dp[i][0]=dp[i][1]+max(dp[儿子节点][0],dp[儿子节点][1]);
 
 #include <iostream>
#include <cstdio>
#include <vector>
#include <queue>
#include <map>
#include <cmath>
#include <stack>
#include <cstring>
#include <algorithm>
#include <cstdlib>
#define FOR(i,x,n) for(long i=x;i<n;i++)
#define ll long long int
#define INF 0x3f3f3f3f
#define MOD 1000000007
#define MAX_N 60
#define MAX_M 1005 using namespace std; struct node{
int number;
int rating;
int sonNum;
int son[];
int father;
};
node tree[];
//int visable[6005];
int dp[][];//0表示不去,1表示去 void dfs(int root){
FOR(i,,tree[root].sonNum){
dfs(tree[root].son[i]);
dp[root][]+=max(dp[tree[root].son[i]][],dp[tree[root].son[i]][]);
dp[root][]+=dp[tree[root].son[i]][];
}
} int main()
{
//freopen("input1.txt", "r", stdin);
//freopen("data.out", "w", stdout);
int n;
//memset(visable,0,sizeof(visable));
memset(dp,,sizeof(dp));
scanf("%d",&n);
FOR(i,,n+){
tree[i].father=i;
tree[i].sonNum=;
}
FOR(i,,n+){
scanf("%d",&dp[i][]);
}
int t1,t2;
FOR(i,,n){
scanf("%d %d",&t1,&t2);
if(!(t1+t2)){
break;
}
tree[t1].father=t2;
tree[t2].son[tree[t2].sonNum++]=t1;
}
int t=;
while(tree[t].father!=t){
t=tree[t].father;
}
dfs(t);
printf("%d",max(dp[t][],dp[t][]));
//fclose(stdin);
//fclose(stdout);
return ;
}

poj2342 Anniversary party的更多相关文章

  1. poj2342 Anniversary party (树形dp)

    poj2342 Anniversary party (树形dp) Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9128   ...

  2. [poj2342]Anniversary party_树形dp

    Anniversary party poj-2342 题目大意:没有上司的舞会原题. 注释:n<=6000,-127<=val<=128. 想法:其实就是最大点独立集.我们介绍树形d ...

  3. DP Intro - Tree POJ2342 Anniversary party

    POJ 2342 Anniversary party (树形dp 入门题) Anniversary party Time Limit: 1000MS   Memory Limit: 65536K To ...

  4. POJ2342 Anniversary party(动态规划)(树形DP)

    Anniversary party Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6635   Accepted: 3827 ...

  5. 树形dp poj2342 Anniversary party * 求最大价值

    Description There is going to be a party to celebrate the 80-th Anniversary of the Ural State Univer ...

  6. poj2342 Anniversary party【树形dp】

    转载请注明出处,谢谢:http://www.cnblogs.com/KirisameMarisa/p/4316097.html   ---by 墨染之樱花 [题目链接]http://poj.org/p ...

  7. [poj2342]Anniversary party树形dp入门

    题意:选出不含直接上下司关系的最大价值. 解题关键:树形dp入门题,注意怎么找出根节点,运用了并查集的思想. 转移方程:dp[i][1]+=dp[j][0];/i是j的子树 dp[i][0]+=max ...

  8. 【poj2342】 Anniversary party

    http://poj.org/problem?id=2342 (题目链接) 题意 没有上司的舞会... Solution 树形dp入门题. dp[i][1]表示第i个节点的子树当节点i去时的最大值,d ...

  9. Anniversary party(hdu1520)(poj2342)题解

    原题地址:http://poj.org/problem?id=2342 题目大意: 上司和下属不能同时参加派对,求参加派对的最大活跃值. 关系满足一棵树,每个人都有自己的活跃值(-128~127) 求 ...

随机推荐

  1. [Java代码] Java用pinyin4j根据汉语获取各种格式和需求的拼音

    pinyin4j是一个功能强悍的汉语拼音工具包,主要是从汉语获取各种格式和需求的拼音,功能强悍,下面看看如何使用pinyin4j.下面介绍用pinyin4j来做的一个根据汉语获取各种格式和需求的拼音d ...

  2. jitwatch查看JIT后的汇编码

    1.下载Apache Maven 3.5.3 http://maven.apache.org/download.cgi apache-maven-3.5.3-bin.zip 解压为C:\maven3 ...

  3. 无法打开运行空间池,服务器管理器winrm插件可能已损坏或丢失

    在使用windows2012 的服务器或云主机时,服务器安装不了iis服务. 提示 “无法打开运行空间池,服务器管理器winrm插件可能已损坏或丢失”. 这个问题可能的原因是您的机器未设置虚拟内存,可 ...

  4. CSS魔法堂:更丰富的前端动效by CSS Animation

    前言  在<CSS魔法堂:Transition就这么好玩>中我们了解到对于简单的补间动画,我们可以通过transition实现.那到底多简单的动画适合用transtion来实现呢?答案就是 ...

  5. ReactNative如何在JS中引用原生自定义控件(rn变化太快,网上很多教程有坑,这个我研究后可用,特意分享)

    直接写一个Demo例子,有相关功底的肯定明白,会对特别的地方进行提醒,本文基于https://blog.csdn.net/lintcgirl/article/details/53489490,但是按此 ...

  6. 【.NET 深呼吸】.net core 中的轻量级 Composition

    记得前面老周写过在.net core 中使用 Composition 的烂文.上回老周给大伙伴们介绍的是一个“重量级”版本—— System.ComponentModel.Composition.应该 ...

  7. DockerSwarm获取Token与常用命令

    一.Token相关 Join tokens是允许一个节点加入集群的密钥.有两种可用的不同的join tokens,一个是用作worker角色,另一个是用作manager角色.在执行swarm join ...

  8. Hibernate 离线对象构建通用查询

    1.业务场景 当下主系统衍生子业务系统已经成为常态,像京东的物流和金融,阿里的支付宝和淘宝. 子业务系统需要对主系统的资源进行访问,这里的资源我具体化为数据库数据,但日常业务中可能不只是数据. 抽象服 ...

  9. 【PHP】解析PHP中的函数

    目录结构: contents structure [-] 可变参数的函数 变量函数 回调函数 自定义函数库 闭包(Closure)函数的使用 在这篇文章中,笔者将会讲解如何使用PHP中的函数,PHP是 ...

  10. H+ 编辑tab页 保存后 刷新列表tab页 并关闭自已。tabA页调用tabB页的方法

    //注:在contabs.js文件中 $(function () { }); 方法外 加入 //注: data-name="' + menuName + '" 这句是加入的自定义属 ...