今天开始做老师给的专辑,打开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:

    1. #include<cmath>
    2. #include<algorithm>
    3. #include<vector>
    4. #include<cstdio>
    5. #include<cstdlib>
    6. #include<cstring>
    7. #include<string>
    8. using namespace std;
    9. #define maxn 6005
    10. int n;
    11. int dp[maxn][2],father[maxn];//dp[i][0]0表示不去,dp[i][1]1表示去了
    12. bool visited[maxn];
    13. void tree_dp(int node)
    14. {
    15. int i;
    16. visited[node] = 1;
    17. for(i=1; i<=n; i++)
    18. {
    19. if(!visited[i]&&father[i] == node)//i为下属
    20. {
    21. tree_dp(i);//递归调用孩子结点,从叶子结点开始dp
    22. //关键
    23. dp[node][1] += dp[i][0];//上司来,下属不来
    24. dp[node][0] +=max(dp[i][1],dp[i][0]);//上司不来,下属来、不来
    25. }
    26. }
    27. }
    28. int main()
    29. {
    30. int i;
    31. int f,c,root;
    32. while(scanf("%d",&n)!=EOF)
    33. {
    34. memset(dp,0,sizeof(dp));
    35. memset(father,0,sizeof(father));
    36. memset(visited,0,sizeof(visited));
    37. for(i=1; i<=n; i++)
    38. {
    39. scanf("%d",&dp[i][1]);
    40. }
    41. root = 0;//记录父结点
    42. bool beg = 1;
    43. while (scanf("%d %d",&c,&f),c||f)
    44. {
    45. father[c] = f;
    46. if( root == c || beg )
    47. {
    48. root = f;
    49. }
    50. }
    51. while(father[root])//查找父结点
    52. root=father[root];
    53. tree_dp(root);
    54. int imax=max(dp[root][0],dp[root][1]);
    55. printf("%d\n",imax);
    56. }
    57. return 0;
    58. }

DP Intro - poj 2342 Anniversary party的更多相关文章

  1. DP Intro - Tree POJ2342 Anniversary party

    POJ 2342 Anniversary party (树形dp 入门题) Anniversary party Time Limit: 1000MS   Memory Limit: 65536K To ...

  2. 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 ...

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

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

  4. POJ 2342 Anniversary party (树dp)

    题目链接:http://poj.org/problem?id=2342 有n个人,每个人有活跃值.下面n-1行u和v表示u的上司是v,有直接上司和下属的关系不能同时参加party,问你party最大的 ...

  5. poj 2342 Anniversary party 树形DP入门

    题目链接:http://poj.org/problem?id=2342 题意:一家公司有1 <= N <= 6 000个职工,现要组织一些职工参加晚会,要求每个职工和其顶头上司不能同时参加 ...

  6. POJ 2342 Anniversary party 树形DP基础题

    题目链接:http://poj.org/problem?id=2342 题目大意:在一个公司中,每个职员有一个快乐值ai,现在要开一个party,邀请了一个员工就不可能邀请其直属上司,同理邀请了一个人 ...

  7. POJ 2342 Anniversary party(树形dp)

    Anniversary party Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7230   Accepted: 4162 ...

  8. poj 2342 Anniversary party 简单树形dp

    Anniversary party Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3862   Accepted: 2171 ...

  9. [ACM] POJ 2342 Anniversary party (树DP获得冠军)

    Anniversary party Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4410   Accepted: 2496 ...

随机推荐

  1. 团体程序设计天梯赛L1-024 后天 2017-03-22 17:59 68人阅读 评论(0) 收藏

    L1-024. 后天 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 如果今天是星期三,后天就是星期五:如果今天是星期六,后天就 ...

  2. python 爬虫登录保存会话去获取只有登录能获取的数据

    #!/usr/bin/env python # -*- coding: utf-8 -*- # import ConfigParser import datetime import sys impor ...

  3. [SIP01]SIP Header Fields里面各字段用途

    INVITE sip:bob@biloxi.com SIP/2.0 Via: SIP/2.0/UDP pc33.atlanta.com;branch=z9hG4bK776asdhds Max-Forw ...

  4. word2010多级列表编号为什么会变成黑块

    把光标放置在黑块的后面 在键盘上按左方向键,则黑块变灰色(为选中状态) 然后ctrl+shift+s, 出现窗口“apply styles" 点击"reapply", 搞 ...

  5. Spreadsheet 常用属性

    标题栏是否可见 Spreadsheet1.TitleBar.Visible=true 标题栏背景颜色 Spreadsheet1.TitleBar.Interior.Color="Green& ...

  6. 使用System.Data.SQLite及其EF模块操作SQLite数据库(文件)

    SQLite for .NET (System.Data.SQLite) introduction An ADO.NET provider for SQLite including EF and Li ...

  7. MYSQL ZIP 压缩包 安装

    基win10安装 1.管理员身份打开cmd 2.进入mysql 的 bin文件下  D: 回车 3.mysql服务名字  -install  回车 4.net start mysql服务名字     ...

  8. 中文 bootstrapValidator

    官网地址:http://bassistance.de/jquery-plugins/jquery-plugin-validation jQuery plugin: Validation 使用说明 转载 ...

  9. 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 ...

  10. POJ - 2031C - Building a Space Station最小生成树

    You are a member of the space station engineering team, and are assigned a task in the construction ...