zoukankan      html  css  js  c++  java
  • HDU 1520 Anniversary party 树形dp

    Anniversary party

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 2225    Accepted Submission(s): 963


    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
    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
     1 // 题意:一棵树每个节点有一个权值,保证父亲节点与孩子节点不能同时出现,求出现的节点最大权值。
     2 // 基础树形dp。
     3 #include <iostream>
     4 using namespace std ;
     5 
     6 struct Tree                 //用孩子兄弟表示法存储
     7 {
     8     int father ;
     9     int child ;
    10     int brother ;
    11     int with_max;    //表示选择该节点的子树所得的最大值
    12     int without_max;  //表示不选该节点的子树所得的最大值
    13     int MAX()       //现在再看这些写法,感觉很熟悉了。
    14     {
    15         return with_max>=without_max?with_max:without_max;
    16     }
    17     void init()
    18     {
    19         father=child=brother=without_max=0 ;
    20     }
    21 }tree[6001] ;
    22 
    23 void dfs(int id)
    24 {
    25     int child;
    26     child=tree[id].child;
    27     while(child)
    28     {
    29         dfs(child);
    30         tree[id].with_max+=tree[child].without_max;  //dp---有父亲一定不能有儿子
    31         tree[id].without_max+=tree[child].MAX();  //这里的tree[child].MAX()不能写成tree[child].with_max,因为没父亲儿子可有可无;
    32         child=tree[child].brother;
    33     }
    34 }
    35 
    36 int main()
    37 {
    38     int n,i;
    39     while(~scanf("%d",&n))
    40     {
    41         for(i=1;i<=n;i++)
    42         {
    43             scanf("%d",&tree[i].with_max);
    44             tree[i].init();
    45         }
    46         int a,b;
    47         while(scanf("%d%d",&a,&b),a||b)  //构造孩子兄弟树----很巧妙,注意方法,
    48         {                                //与书上讲的孩子兄弟树的存储方法不同不同
    49             tree[a].father=b;
    50             tree[a].brother=tree[b].child;
    51             tree[b].child=a; 
    52         }
    53         for(i=1;i<=n;i++)
    54             if(!tree[i].father)  //题目中说的是一个学校的组织,应该不会出现单独的部门吧,
    55             {                        //所以只会出现一棵树,即只有一根
    56                 dfs(i) ;
    57                 printf("%d\n",tree[i].MAX());
    58                 break ;
    59             }
    60     }
    61     return 0 ;
    62 }
    功不成,身已退
  • 相关阅读:
    推荐7个GitHub上不错的Python机器学习项目
    值得收藏的45个Python优质资源
    9 个鲜为人知的 Python 数据科学库
    小众Python库介绍
    Sebastian Ruder : NLP 领域知名博主博士论文面向自然语言处理的神经网络迁移学习
    学习Python 新去处:Python 官方中文文档
    图像超分辨率项目帮你「拍」出高清照片
    入坑机器学习?听听MIT在读博士的AI心得
    自然语言处理领域重要论文&资源全索引
    旷视等Oral论文提出GeoNet:基于测地距离的点云分析深度网络
  • 原文地址:https://www.cnblogs.com/dongsheng/p/2731908.html
Copyright © 2011-2022 走看看