DP Intro - poj 2342 Anniversary party
今天开始做老师给的专辑,打开DP专辑 A题 Rebuilding Roads 直接不会了,发现是树形DP,百度了下了该题,看了老半天看不懂,想死的冲动都有了~~~~
最后百度了下,树形DP入门,找到了 poj 2342 Anniversary party 先入门一下~
题意:
某公司要举办一次晚会,但是为了使得晚会的气氛更加活跃,每个参加晚会的人都不希望在晚会中见到他的直接上司,现在已知每个人的活跃指数和上司关系(当然不可能存在环),求邀请哪些人(多少人)来能使得晚会的总活跃指数最大。
思路:
任何一个点的取舍可以看作一种决策,那么状态就是在某个点取的时候或者不取的时候,以他为根的子树能有的最大活跃总值。分别可以用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的下属
以下代码参考:http://hi.baidu.com/saintlleo/blog/item/0606b3feb7026ad3b48f3111.html
//AC CODE:
- #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][2],father[maxn];//dp[i][0]0表示不去,dp[i][1]1表示去了
- bool visited[maxn];
- void tree_dp(int node)
- {
- int i;
- visited[node] = 1;
- for(i=1; i<=n; i++)
- {
- if(!visited[i]&&father[i] == node)//i为下属
- {
- tree_dp(i);//递归调用孩子结点,从叶子结点开始dp
- //关键
- dp[node][1] += dp[i][0];//上司来,下属不来
- dp[node][0] +=max(dp[i][1],dp[i][0]);//上司不来,下属来、不来
- }
- }
- }
- int main()
- {
- int i;
- int f,c,root;
- while(scanf("%d",&n)!=EOF)
- {
- memset(dp,0,sizeof(dp));
- memset(father,0,sizeof(father));
- memset(visited,0,sizeof(visited));
- for(i=1; i<=n; i++)
- {
- scanf("%d",&dp[i][1]);
- }
- root = 0;//记录父结点
- bool beg = 1;
- while (scanf("%d %d",&c,&f),c||f)
- {
- father[c] = f;
- if( root == c || beg )
- {
- root = f;
- }
- }
- while(father[root])//查找父结点
- root=father[root];
- tree_dp(root);
- int imax=max(dp[root][0],dp[root][1]);
- printf("%d\n",imax);
- }
- return 0;
- }
DP Intro - poj 2342 Anniversary party的更多相关文章
- DP Intro - Tree POJ2342 Anniversary party
POJ 2342 Anniversary party (树形dp 入门题) Anniversary party Time Limit: 1000MS Memory Limit: 65536K To ...
- POJ 2342 Anniversary party / HDU 1520 Anniversary party / URAL 1039 Anniversary party(树型动态规划)
POJ 2342 Anniversary party / HDU 1520 Anniversary party / URAL 1039 Anniversary party(树型动态规划) Descri ...
- POJ 2342 - Anniversary party - [树形DP]
题目链接:http://poj.org/problem?id=2342 Description There is going to be a party to celebrate the 80-th ...
- POJ 2342 Anniversary party (树dp)
题目链接:http://poj.org/problem?id=2342 有n个人,每个人有活跃值.下面n-1行u和v表示u的上司是v,有直接上司和下属的关系不能同时参加party,问你party最大的 ...
- poj 2342 Anniversary party 树形DP入门
题目链接:http://poj.org/problem?id=2342 题意:一家公司有1 <= N <= 6 000个职工,现要组织一些职工参加晚会,要求每个职工和其顶头上司不能同时参加 ...
- POJ 2342 Anniversary party 树形DP基础题
题目链接:http://poj.org/problem?id=2342 题目大意:在一个公司中,每个职员有一个快乐值ai,现在要开一个party,邀请了一个员工就不可能邀请其直属上司,同理邀请了一个人 ...
- POJ 2342 Anniversary party(树形dp)
Anniversary party Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7230 Accepted: 4162 ...
- poj 2342 Anniversary party 简单树形dp
Anniversary party Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3862 Accepted: 2171 ...
- [ACM] POJ 2342 Anniversary party (树DP获得冠军)
Anniversary party Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4410 Accepted: 2496 ...
随机推荐
- 团体程序设计天梯赛L1-024 后天 2017-03-22 17:59 68人阅读 评论(0) 收藏
L1-024. 后天 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 如果今天是星期三,后天就是星期五:如果今天是星期六,后天就 ...
- python 爬虫登录保存会话去获取只有登录能获取的数据
#!/usr/bin/env python # -*- coding: utf-8 -*- # import ConfigParser import datetime import sys impor ...
- [SIP01]SIP Header Fields里面各字段用途
INVITE sip:bob@biloxi.com SIP/2.0 Via: SIP/2.0/UDP pc33.atlanta.com;branch=z9hG4bK776asdhds Max-Forw ...
- word2010多级列表编号为什么会变成黑块
把光标放置在黑块的后面 在键盘上按左方向键,则黑块变灰色(为选中状态) 然后ctrl+shift+s, 出现窗口“apply styles" 点击"reapply", 搞 ...
- Spreadsheet 常用属性
标题栏是否可见 Spreadsheet1.TitleBar.Visible=true 标题栏背景颜色 Spreadsheet1.TitleBar.Interior.Color="Green& ...
- 使用System.Data.SQLite及其EF模块操作SQLite数据库(文件)
SQLite for .NET (System.Data.SQLite) introduction An ADO.NET provider for SQLite including EF and Li ...
- MYSQL ZIP 压缩包 安装
基win10安装 1.管理员身份打开cmd 2.进入mysql 的 bin文件下 D: 回车 3.mysql服务名字 -install 回车 4.net start mysql服务名字 ...
- 中文 bootstrapValidator
官网地址:http://bassistance.de/jquery-plugins/jquery-plugin-validation jQuery plugin: Validation 使用说明 转载 ...
- 6.iptables常用规则
开启ip段192.168.1.0/24端的80口 开启ip段211.123.16.123/24端ip段的80口 # iptables -I INPUT -p tcp --dport 80 -j DRO ...
- POJ - 2031C - Building a Space Station最小生成树
You are a member of the space station engineering team, and are assigned a task in the construction ...