poj2342 Anniversary party (树形dp)

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 9128   Accepted: 5250

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

大致题意:

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

思路:

思路:

任何一个点的取舍可以看作一种决策,那么状态就是在某个点取的时候或者不取的时候,以他为根的子树能有的最大活跃总值。分别可以用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<cmath>
#include<algorithm>
#include<vector>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string> using namespace std; #define maxn 6005 int n;
int dp[maxn][],father[maxn];//dp[i][0]0表示不去,dp[i][1]1表示去了
bool visited[maxn]; void tree_dp(int node)
{
int i;
visited[node] = ;
for(i=; i<=n; i++) //找1到n中找node节点的下属
{
if(!visited[i]&&father[i] == node)//i为下属
{
tree_dp(i);//递归调用孩子结点,从叶子结点开始dp
//关键
dp[node][] += dp[i][];//上司来,下属不来
dp[node][] +=max(dp[i][],dp[i][]);//上司不来,下属来、不来
}
}
} int main()
{
int i;
int f,c,root;
while(scanf("%d",&n)!=EOF)
{
memset(dp,,sizeof(dp));
memset(father,,sizeof(father));
memset(visited,,sizeof(visited));
for(i=; i<=n; i++)
{
scanf("%d",&dp[i][]);
}
root = ;//记录父结点
bool beg = ;
while (scanf("%d %d",&c,&f),c||f)
{
//c的老爸是f
father[c] = f;
//儿子是跟节点 或者这条边是第一条边
//父亲做根节点
if( root == c || beg )
{
root = f;
}
}
//循环寻找根节点
while(father[root])//查找父结点
root=father[root];
//从根节点开始树形dp
tree_dp(root);
//取最大的上司去或者不去中的大值
int imax=max(dp[root][],dp[root][]);
printf("%d\n",imax);
}
return ; }

poj2342 Anniversary party (树形dp)的更多相关文章

  1. [poj2342]Anniversary party_树形dp

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

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

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

  3. poj 2324 Anniversary party(树形DP)

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

  4. POJ 2342 - Anniversary party - [树形DP]

    题目链接:http://poj.org/problem?id=2342 Description There is going to be a party to celebrate the 80-th ...

  5. hdu Anniversary party 树形DP,点带有值。求MAX

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

  6. POJ 2342 &&HDU 1520 Anniversary party 树形DP 水题

    一个公司的职员是分级制度的,所有员工刚好是一个树形结构,现在公司要举办一个聚会,邀请部分职员来参加. 要求: 1.为了聚会有趣,若邀请了一个职员,则该职员的直接上级(即父节点)和直接下级(即儿子节点) ...

  7. HDU 1520 Anniversary party [树形DP]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1520 题目大意:给出n个带权点,他们的关系可以构成一棵树,问从中选出若干个不相邻的点可能得到的最大值为 ...

  8. POJ Anniversary party 树形DP

    /* 树形dp: 给一颗树,要求一组节点,节点之间没有父子关系,并且使得所有的节点的权值和最大 对于每一个节点,我们有两种状态 dp[i][0]表示不选择节点i,以节点i为根的子树所能形成的节点集所能 ...

  9. Anniversary party_树形DP

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

  10. HDU1520 Anniversary party 树形DP基础

    There is going to be a party to celebrate the 80-th Anniversary of the Ural State University. The Un ...

随机推荐

  1. [Windows Server 2008] 查看ASP详细错误信息方法

    ★ 欢迎来到[护卫神·V课堂],网站地址:http://v.huweishen.com ★ 护卫神·V课堂是护卫神旗下专业提供服务器教学视频的网站,每周更新视频. ★ 本节我们将带领大家:查看IIS下 ...

  2. (转)Quartz任务调度(1)概念例析快速入门

    http://blog.csdn.net/qwe6112071/article/details/50991563 Quartz框架需求引入 在现实开发中,我们常常会遇到需要系统在特定时刻完成特定任务的 ...

  3. 基于zk“临时顺序节点“的分布式锁

    import org.apache.zookeeper.*; import org.apache.zookeeper.data.Stat; import java.io.IOException; im ...

  4. for mxl path

    废话不多说,直接上例子 简单明了 create table tb_class ( classId int , className ) ) go ,'一班') ,'二班') ,'三班') go crea ...

  5. 解决 The file will have its original line endings in your working directory

    首先出现这个问题主要原因是:我们从别人github地址上通过git clone下载下来,而又想git push到我们自己的github上,那么就会出现上面提示的错误信息 此时需要执行如下代码: git ...

  6. 怎么用最短时间高效而踏实地学习Linux?

    在技术行业里,人才的唯一衡量标准就是技术能力,而技术能力,就代表着你的薪资.职位.话语权.很多人都经历过,跟自己同时入行甚至入行还晚的人,成长速度却远超自己,短短两三年就拉开了差距. 秘密就在于,有些 ...

  7. 【原】Python学习_Django搭建环境及创建第一个项目

    1.Window 平台安装 Python 下载安装包    https://www.python.org/downloads/windows/ 2.Pyhton环境变量配置 右键点击"计算机 ...

  8. IDEA生成增强for循环

    itar 生成array for代码块 for (int i = 0; i < array.length; i++) { = array[i]; } itco 生成Collection迭代 fo ...

  9. JAVA经典题--计算一个字符串中每个字符出现的次数

    需求:  计算一个字符串中每个字符出现的次数 思路: 通过toCharArray()拿到一个字符数组--> 遍历数组,将数组元素作为key,数值1作为value存入map容器--> 如果k ...

  10. selenium常用操作,查找元素,操作Cookie,获取截图,获取窗口信息,切换,执行js代码

    目录: 1. 常用操作 2. 查找元素 3. 操作Cookie 4. 获取截图 5. 获取窗口信息 6. 切换 7. 执行JS代码 简介 selenium.webdriver.remote.webdr ...