Anniversary party

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

Total Submission(s): 6271    Accepted Submission(s): 2820

Problem 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 T 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
  1. 7
  2. 1
  3. 1
  4. 1
  5. 1
  6. 1
  7. 1
  8. 1
  9. 1 3
  10. 2 3
  11. 6 4
  12. 7 4
  13. 4 5
  14. 3 5
  15. 0 0
 
Sample Output
  1. 5
 
Source
 
Recommend
linle   |   We have carefully selected several similar problems for you:  2196 1494 2242 

pid=3001" target="_blank">3001 

pid=1074" target="_blank">1074 

题目大意:有n个人去參加party,每一个人都有自己的价值。后边跟很多行a b(0,0结束),表示b是a的上司,不能使他和他的上司同一时候參加,能够获得最大的快乐值,
dp[i][0]表示他不參加的最大快乐值,dp[i][1]表示他參加最大的快乐值
 ac代码
  1. #include<stdio.h>
  2. #include<string.h>
  3. #define max(a,b) (a>b?
  4.  
  5. a:b)
  6. struct s
  7. {
  8. int u,v,next;
  9. }edge[3000100];
  10. int a[6060],dp[6060][2],dig[6060],head[6060],vis[6060],cnt;
  11. void add(int u,int v)
  12. {
  13. edge[cnt].u=u;
  14. edge[cnt].v=v;
  15. edge[cnt].next=head[u];
  16. head[u]=cnt++;
  17. }
  18. void dfs(int u)
  19. {
  20. vis[u]=1;
  21. dp[u][1]=a[u];
  22. int i;
  23. for(i=head[u];i!=-1;i=edge[i].next)
  24. {
  25. int v=edge[i].v;
  26. if(!vis[v])
  27. {
  28. dfs(v);
  29. dp[u][0]+=max(dp[v][0],dp[v][1]);
  30. dp[u][1]+=dp[v][0];
  31. }
  32. }
  33. }
  34. int main()
  35. {
  36. int n;
  37. while(scanf("%d",&n)!=EOF)
  38. {
  39. int i;
  40. for(i=1;i<=n;i++)
  41. {
  42. scanf("%d",&a[i]);
  43. }
  44. int u,v;
  45. cnt=0;
  46. memset(head,-1,sizeof(head));
  47. memset(dp,0,sizeof(dp));
  48. memset(dig,0,sizeof(dig));
  49. memset(vis,0,sizeof(vis));
  50. while(scanf("%d%d",&v,&u),u||v)
  51. {
  52. add(u,v);
  53. dig[v]++;
  54. }
  55. int s;
  56. for(i=1;i<=n;i++)
  57. {
  58. if(!dig[i])
  59. {
  60. s=i;
  61. }
  62. }
  63. dfs(s);
  64. printf("%d\n",max(dp[s][0],dp[s][1]));
  65. }
  66. }


HDOJ 题目1520 Anniversary party(树形dp)的更多相关文章

  1. HDU 1520 Anniversary party [树形DP]

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

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

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

  3. hdu oj 1520 Anniversary party(树形dp入门)

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

  4. poj 2324 Anniversary party(树形DP)

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

  5. [poj2342]Anniversary party_树形dp

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

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

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

  7. HDU1520 Anniversary party —— 树形DP

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1520 Anniversary party Time Limit: 2000/1000 MS (Java ...

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

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

  9. 【HDOJ】1520 Anniversary party

    第二道树形DP,先是MLE.后来仅需改小邻接矩阵的第二个维度到30就过了. #include <cstdio> #include <cstring> #include < ...

随机推荐

  1. laydate 监听日期切换

    ```` //日期范围 laydate.render({ elem: '#Time', range: "至", max: gitData() ,done: function(val ...

  2. python_for循环

    #for循环'''for i in range(0,10,2):age_oldboy = 56for i in range(3): guess_age = int(input("guess ...

  3. 【【henuacm2016级暑期训练】动态规划专题 L】Civilization

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 给一个森林. 就是由很多棵树组成.. 然后会询问你其中一棵树的最长链. 初始状态的最长链可以用两遍dfs分别找最长路得到. 然后要求 ...

  4. MyBatis学习总结(2)——使用MyBatis对表执行CRUD操作

    一.使用MyBatis对表执行CRUD操作--基于XML的实现 1.定义sql映射xml文件 userMapper.xml文件的内容如下: <?xml version="1.0&quo ...

  5. FastDFS 实现图片上传_01

    一.jar 包 jar包下载:https://pan.baidu.com/s/1nwkAHU5 密码:tlv6 或者 下载工程,安装到 maven 本地仓库 工程下载:https://pan.baid ...

  6. linux下使用DBCA(database configuration assistant)创建oracle数据库

    前提:切换到图形界面 到Oracle的bin文件夹下,使用oracle用户.运行dbca就可以.和windows的效果一样. 假设出现乱码 export LANG="en_US:UTF-8& ...

  7. Django连接mysql

    链接文档地址:https://docs.djangoproject.com/zh-hans/2.0/intro/tutorial02/ 由于我使用的是mysql,所以设置的是mysql的: 在mysl ...

  8. BZOJ 1007: [HNOI2008]水平可见直线 平面直线

    1007: [HNOI2008]水平可见直线 Description 在xoy直角坐标平面上有n条直线L1,L2,...Ln,若在y值为正无穷大处往下看,能见到Li的某个子线段,则称Li为可见的,否则 ...

  9. No connection could be made because the target machine actively refused it [::1]:808

    No connection could be made because the target machine actively refused it [::1]:808 1.首先查看端口占用情况, 在 ...

  10. zzulioj--1791-- 旋转矩阵(模拟水题)

     旋转矩阵 Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 268  Solved: 116 SubmitStatusWeb Board Descr ...