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. 完整版本的停车场管理系统源代码带服务端+手机android客户端

    该源码是停车场管理软件附带源代码 J2EE服务端+android客户端,也是一套停车场管理车辆进出的管理软,喜欢的朋友可以看看吧. 应用的后台管理主要功能介绍:1  机构管理 ,机构有从属管理< ...

  2. JS高级——arguments

    arguments 1.函数内部的一个对象,在函数调用的时候,默认的会将所有传入的实参依次存入该对象 2.是一个伪数组 3.arguments.length 可以用来表示传入实参的个数 4.argum ...

  3. Please, commit your changes or stash them before you can merge

    参照 : https://blog.csdn.net/iefreer/article/details/7679631 用git pull来更新代码的时候,遇到了下面的问题: error: Your l ...

  4. Django - orm外键操作

    1.orm外键操作 创建外键: 备注:ForeignKey两个参数,1个为关联的表名,1个为关联的字段名: 在django2.0后,定义外键和一对一关系的时候需要加on_delete选项,此参数为了避 ...

  5. 在引入的css或者js文件后面加参数的作用

    有时候可能会遇到js或者css文件引用后传递参数: css和js带参数(形如.css?v=与.js?v=) <script type=”text/javascript” src=”jb51.js ...

  6. uva 10048 Audiophobia UVA - 10048

    题目简介 一个无向正权图,求任意两个节点之间的路径里最短的路径长度. 直接Floyd解决,就是注意要把Floyd的DP式子改一下成 G[i][j]=min(G[i][j],max(G[i][k],G[ ...

  7. JS练习:两级联动

    代码: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title ...

  8. JavaScript控制iframe中元素的样式

    //根据ID获取要操控元素 var deptObjs=document.getElementById("IFRAMEID").contentWindow.document.getE ...

  9. 1.scrapy爬取的数据保存到es中

    先建立es的mapping,也就是建立在es中建立一个空的Index,代码如下:执行后就会在es建lagou 这个index.     from datetime import datetime fr ...

  10. 联想小新Air 15 安装黑苹果macOS High Sierra 10.13.6过程

    联想小新Air 15 安装黑苹果全过程 本文参考:https://blog.csdn.net/qq_28735663/article/details/80634300 本人是联想小新AIr 15 , ...