Anniversary party
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 5767   Accepted: 3335

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

题意:

某公司要举办一次晚会,但是为了使得晚会的气氛更加活跃,每个参加晚会的人都不希望在晚会中见到他的直接上司,现在已知每个人的活跃指数和上司关系(当然不可能存在环),求邀请哪些人(多少人)来能使得晚会的总活跃指数最大。

思路:

任何一个点的取舍可以看作一种决策,那么状态就是在某个点取的时候或者不取的时候,以他为根的子树能有的最大活跃总值。分别可以用f[i,1]和f[i,0]表示第i个人来和不来。

当i来的时候,dp[i][1] += dp[j][0];//j为i的下属

当i不来的时候,dp[i][0] +=max(dp[j][1],dp[j][0]);//j为i的下属


 #include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;
#define Max 6005
bool vis[Max];
int dp[Max][],fa[Max],num[Max];
int n;
void tree_dp(int node)
{
int i,j;
vis[node]=;
for(i=;i<=n;i++)
{
if(vis[i]==&&fa[i]==node)
{
tree_dp(i);
dp[node][]+=dp[i][];
dp[node][]+=max(dp[i][],dp[i][]);
}
}
}
int main()
{
int i,j;
int a,b;
int root;
freopen("in.txt","r",stdin);
while(scanf("%d",&n)!=EOF)
{
memset(vis,,sizeof(vis));
memset(dp,,sizeof(dp));
memset(fa,,sizeof(fa));
for(i=;i<=n;i++)
scanf("%d",&dp[i][]);
while(scanf("%d%d",&a,&b))
{
if(a==&&b==)
break;
fa[a]=b; //a的父节点是b
}
root=;
while(fa[root]!=)
root=fa[root];
tree_dp(root);
cout<<max(dp[root][],dp[root][])<<endl;;
}
}

												

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

  1. POJ 2342 树形DP入门题

    有一个大学的庆典晚会,想邀请一些在大学任职的人来參加,每一个人有自己的搞笑值,可是如今遇到一个问题就是假设两个人之间有直接的上下级关系,那么他们中仅仅能有一个来參加,求请来一部分人之后,搞笑值的最大是 ...

  2. POJ 2342 (树形DP)

    题目链接: http://poj.org/problem?id=2342 题目大意:直属上司和下属出席聚会.下属的上司出现了,下属就不能参加,反之下属参加.注意上司只是指直属的上司.每个人出席的人都有 ...

  3. poj 2342树形dp板子题1

    http://poj.org/problem?id=2342 #include<iostream> #include<cstdio> #include<cstring&g ...

  4. Fire (poj 2152 树形dp)

    Fire (poj 2152 树形dp) 给定一棵n个结点的树(1<n<=1000).现在要选择某些点,使得整棵树都被覆盖到.当选择第i个点的时候,可以覆盖和它距离在d[i]之内的结点,同 ...

  5. Anniversary party POJ - 2342 (树形DP)

    题目链接:  POJ - 2342 题目大意:给你n个人,然后每个人的重要性,以及两个人之间的附属关系,当上属选择的时候,他的下属不能选择,只要是两个人不互相冲突即可.然后问你以最高领导为起始点的关系 ...

  6. poj 1463(树形dp)

    题目链接:http://poj.org/problem?id=1463 思路:简单树形dp,如果不选父亲节点,则他的所有的儿子节点都必须选,如果选择了父亲节点,则儿子节点可选,可不选,取较小者. #i ...

  7. poj 2486( 树形dp)

    题目链接:http://poj.org/problem?id=2486 思路:经典的树形dp,想了好久的状态转移.dp[i][j][0]表示从i出发走了j步最后没有回到i,dp[i][j][1]表示从 ...

  8. poj 3140(树形dp)

    题目链接:http://poj.org/problem?id=3140 思路:简单树形dp题,dp[u]表示以u为根的子树的人数和. #include<iostream> #include ...

  9. HDU 1520.Anniversary party 基础的树形dp

    Anniversary party Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

随机推荐

  1. CloudStack API编程指引

    原文地址:https://cwiki.apache.org/confluence/display/CLOUDSTACK/CloudStack+API+Coding+Guidelines 前言 本文阐述 ...

  2. The JRE could not be found.Edit the server and change the JRE location.

    之前更改了了一个较低的jdk的版本看了看一个项目的代码,不知所云,然后再改回来, 混乱之中只要启动Tomcat就出现这种错误,还是无法找到JRE,最后如此解决: 在Windows->Prefer ...

  3. rlwrap 的安装使用

    rlwrap 的安装使用 在Windows操作系统上,当在DOS命令窗口中运行SQL*Plus的时候,可以使用向上,向下键来跳回之前已经执行过的SQL语句.你可以根据需要修改他们,然后按Enter键重 ...

  4. ACdream 1017 Fast Transportation

    http://acdream.info/problem?pid=1017 题意:给n个点,m条边,K个货物,要从从S到T,每天每条边最多只能经过1次,求要几天能运完 思路:拆成分层图,每层向下一层连边 ...

  5. boost库使用:vs2013下boost::container::vector编译出错解决

    boost版本:boost_1_55_0 bug报告地址 https://svn.boost.org/trac/boost/ticket/9332 出错信息 has_member_function_c ...

  6. 【转】vim文件编码和乱码处理

    原文网址:http://edyfox.codecarver.org/html/vim_fileencodings_detection.html 在 Vim 中,有四个与编码有关的选项,它们是:file ...

  7. uva10245-The Closest Pair Problem(平面上的点分治)

    解析:平面上的点分治,先递归得到左右子区间的最小值d,再处理改区间,肯定不会考虑哪些距离已经大于d的点对,对y坐标归并排序,然后从小到大开始枚举更新d,对于某个点,x轴方向只用考虑[x-d,x+d]( ...

  8. nodejs学习笔记之安装、入门

    由于项目需要,最近开始学习nodejs.在学习过程中,记录一些必要的操作和应该注意的点.       首先是如何安装nodejs环境?(我用的是windows 7环境,所以主要是windows 7的例 ...

  9. mac下识别国产android手机

    mac下识别国产android手机困扰了我很久,这几天总算在google帮助下找到了解决方法. 在~/.android/下找到adb_usb.ini,如果不存在则创建.通过“系统信息”查看到插入的an ...

  10. Android学习总结——本地广播机制

    为了简单解决广播的安全性问题,Android引入了一套本地广播机制,使用这个机制发出的广播只能在程序的内部进行传递,只能接受来自本应用程序发出的广播.否则当我们发送一些携带关键数据的广播可能被截获,一 ...