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


    Anniversary party

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


    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
     
    Source

    题意:n个人(编号1-N),然后n行分表代表第n个人的活跃度,之后若干行 L 和 K(0 0结束),代表K是L的上司,

    问一个聚会中邀请这n个人中的若干,其中不能含直接的上下级关系,可以使聚会中的活跃度最大为多少。

     1 #include <stdio.h>
     2 #include <string.h>
     3 #include <stdlib.h>
     4 #include <iostream>
     5 #include <algorithm>
     6 #include <vector>
     7 #define ll long long
     8 using namespace std;
     9 const int N = 10000;
    10 int dp[N][3],root[N];
    11 vector<int>G[N];
    12 void DFS(int cur)
    13 {
    14     for(int i = 0; i < (int)G[cur].size(); i++)
    15    {
    16     int to = G[cur][i];
    17     DFS(to);
    18     dp[cur][1] += dp[to][0];//该级去,下一级不去
    19     dp[cur][0] += max(dp[to][0],dp[to][1]);//该级不去,下级去不去选最优
    20    }
    21 }
    22 
    23 int main(void)
    24 {
    25     int n;
    26     while(scanf("%d",&n) != EOF)
    27     {
    28         memset(dp,0,sizeof(dp));
    29         for(int i = 1; i <= n; i++)
    30         {
    31             scanf("%d",&dp[i][1]);//代表第几个人去能增加的活跃度
    32             root[i] = i;//并查集的初始化
    33             G[i].clear();
    34         }
    35         int a,b;
    36         while(scanf("%d %d",&a,&b),a != 0 ||b != 0)
    37         {
    38             G[b].push_back(a);//vector建图
    39             root[a] = b;
    40         }
    41         int beg;
    42         for(int i = 1; i <= n; i++)
    43         {
    44             if(root[i] == i)//查找根节点
    45             {
    46                 beg = i;
    47                 break;
    48             }
    49         }
    50         DFS(beg);
    51         printf("%d
    ",max(dp[beg][0],dp[beg][1]));
    52     }
    53     return 0;
    54 }
    
    
    
    
    
  • 相关阅读:
    NVMe固态硬盘工具箱使用说明
    (原创)Python文件与文件系统系列(1)—— file 对象
    Linux系统排查4——网络篇
    Python内置类型——list
    (原)数据结构——线索二叉树
    Python匿名函数——lambda表达式
    Python生成器
    Python内置类型——dict
    Python内置类型——set
    解决Django-1.8.2应用部署到Apache后无法显示admin应用的CSS
  • 原文地址:https://www.cnblogs.com/henserlinda/p/4775383.html
Copyright © 2020-2023  润新知