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. springboot + websocket + spring-messaging实现服务器向浏览器广播式

    目录结构 pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http:// ...

  2. jquery-卡片翻转

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  3. ASM ClassReader failed to parse class file解决方法

    1.  环境信息: Spring 3.2.2,  JDK 1.8, Hibernate 3.5.5 2.  运行简单的程序,出现以下错误信息: [2018-05-25 02:36:58,671] Ar ...

  4. 通过Comparable来实现对自身的比较

    import org.apache.commons.lang.builder.CompareToBuilder; import org.apache.commons.lang.builder.Equa ...

  5. Data Model for Certificate

  6. 小米Pro 安装苹果系统

    参考 http://www.miui.com/thread-11363672-1-1.html http://www.miui.com/thread-7601066-1-1.html https:// ...

  7. ThreadPoolExcutor 线程池 异常处理 (下篇)

    前言 因为这是之前面试的一个题目,所以印象比较深刻,前几天写了一篇文章:ThreadPoolExcutor 线程池 异常处理 (上篇) 中已经介绍了线程池异常的一些问题以及一步步分析了里面的一些源代码 ...

  8. Eclipse安装Git插件及简单操作

    0. 前言 说一件事,说起来也是好笑,工作三年半了,还没接触到团队开发,都是一个人小打小闹.因此连Git都没有使用过.感觉好Low的,这一篇,简单讲一下,Eclipse配置Git插件,并提交代码到Gi ...

  9. 【转】Android系统开篇

    版权声明:本站所有博文内容均为原创,转载请务必注明作者与原文链接,且不得篡改原文内容.另外,未经授权文章不得用于任何商业目的. 一.引言 Android系统非常庞大.错综复杂,其底层是采用Linux作 ...

  10. linux shell命令之wc/split及特殊字符

    [时间:2018-07] [状态:Open] [关键词:linux, wc, split, 通配符,转义符,linux命令] 0 引言 整理这篇文章的目的不是为了什么学习,仅仅是为了强化下记忆,以便下 ...