转载请注明出处,谢谢:http://www.cnblogs.com/KirisameMarisa/p/4316097.html   ---by 墨染之樱花

【题目链接】http://poj.org/problem?id=2342

【题目描述】一个企业有N个员工,每个人都有一个表示欢乐度的数值。现在要开一场party,不过这些员工里面有上下级关系,每个员工都不希望见到自己的直接上司,即每个员工都不能和直接上司同时出现。求这场party到场的人的总欢乐度的最大值

【思路】最基础的树形dp,现根据上下级关系构成树状图森林,然后在每棵树上进行动态规划,dp[i][0]和dp[i][1]表示以i为根的子树分别在i不来和来的情况下的最大总欢乐度。显然有dp[i][1]=sum(dp[j][0])+a[i],dp[i][0]=sum(max(dp[j][0],dp[j][1])),其中j遍历i的所有儿子节点。最后只需求森林中所有根结点的max(dp[r][0],dp[r][1])的总和

#include <iostream>
#include <ios>
#include <iomanip>
#include <functional>
#include <algorithm>
#include <vector>
#include <sstream>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <string>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cmath>
#include <cstring>
#include <climits>
using namespace std;
#define XINF INT_MAX
#define INF 1<<30
#define MAXN 6000+10
#define eps 1e-10
#define zero(a) fabs(a)<eps
#define sqr(a) ((a)*(a))
#define MP(X,Y) make_pair(X,Y)
#define PB(X) push_back(X)
#define PF(X) push_front(X)
#define REP(X,N) for(int X=0;X<N;X++)
#define REP2(X,L,R) for(int X=L;X<=R;X++)
#define DEP(X,R,L) for(int X=R;X>=L;X--)
#define CLR(A,X) memset(A,X,sizeof(A))
#define IT iterator
#define PI acos(-1.0)
#define test puts("OK");
#define _ ios_base::sync_with_stdio(0);cin.tie(0);
typedef long long ll;
typedef pair<int,int> PII;
typedef priority_queue<int,vector<int>,greater<int> > PQI;
typedef vector<PII> VII;
typedef vector<int> VI;
#define X first
#define Y second VI G[MAXN];
int num[MAXN];
int par[MAXN];
int dp[MAXN][];
int N; void dfs(int r)
{
if(G[r].size()==)
{
dp[r][]=;
dp[r][]=num[r];
}
REP(i,G[r].size())
dfs(G[r][i]);
dp[r][]=dp[r][]=;
REP(i,G[r].size())
dp[r][]+=dp[G[r][i]][];
dp[r][]+=num[r];
REP(i,G[r].size())
dp[r][]+=max(dp[G[r][i]][],dp[G[r][i]][]);
} int main()
{_
CLR(par,-);
scanf("%d",&N);
REP(i,N)
scanf("%d",&num[i]);
int x,y;
while(scanf("%d%d",&x,&y))
{
if(x== && y==)
break;
x--;y--;
par[x]=y;
G[y].PB(x);
}
int tot=;
REP(i,N)
if(par[i]==-)
{
dfs(i);
tot+=max(dp[i][],dp[i][]);
}
printf("%d\n",tot);
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. nodejs:注册登录session出错以及连接Mongodb数据库时Error connecting to database解决方案

    (1)nodejs:注册登录session出错 解决办法: 在app.js 中将var MongoStore =  require(connect-mongo')改为var MongoStore =  ...

  2. 【转】linux Centos 6.5 安装桌面环境GNOME

    在某种场合之下,我们使用的Linux还是要选择安装桌面环境的,所以在这里介绍一下如何给没有安装桌面环境的系统安装桌面环境. 以Centos 6.5 为例演示一下如何安装桌面环境. 一.首先查看系统的运 ...

  3. Python 标识符

    Python 标识符 在python里,标识符有字母.数字.下划线组成. 在python中,所有标识符可以包括英文.数字以及下划线(_),但不能以数字开头. python中的标识符是区分大小写的. 以 ...

  4. C/C++指针知识整理(一)

    1.指针(变量)的类型 把指针声明语句里的指针名字去掉,剩下的部分就是这个指针的类型.这是指针本身所具有的类型. (1)int*ptr; //指针的类型是 int* (2) char*ptr;//指针 ...

  5. 一个好用的hash函数(C语言)

    typedef unsigned int DWORD; typedef unsigned char BYTE; /******************************************* ...

  6. MySQL 用户登录与操作执行

    一个用户可以不登录进Mysql 数据库,由两方面的因数决定 1.你是谁:也就是mysql 数据库中记录的用户名和密码,在SQL Server数据库,中只要求说明你是谁就可以登录了,可是mysql 不是 ...

  7. css 样式

    <!doctype html> <html lang="en"> <head> <meta name="Generator&qu ...

  8. Windows Azure 自动伸缩已内置

     WindowsAzure平台提供的主要优点之一是能够在有需要时快速缩放云中的应用程序以响应波动.去年7月以前,您必须编写自定义脚本或使用其他工具(如Wasabi或MetricsHub)来启用自动 ...

  9. POJ 2758 Checking the Text(Hash+二分答案)

    [题目链接] http://poj.org/problem?id=2758 [题目大意] 给出一个字符串,支持两个操作,在任意位置插入一个字符串,或者查询两个位置往后的最长公共前缀,注意查询的时候是原 ...

  10. ssh的学习

    快毕业了.临走前帮导师搭建了gerrit,git服务器,其中涉及ssh的知识,就总结了下.希望对大家有帮助 一.前言(ssh出世的原因) 万物有因就有果,既然ssh存在,就必然有它存在的理由! 许多网 ...