• Anniversary party(hdu1520)


    Anniversary party

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


    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
    思路:树形dp;
    dp[i][j] 表示第j个节点状态为i时i ->>[0,1]该节点选或不选,然后从根节点dfs dp就可以了。复杂度O(n);
     1 #include<stdio.h>
     2 #include<math.h>
     3 #include<queue>
     4 #include<algorithm>
     5 #include<string.h>
     6 #include<iostream>
     7 #include<stack>
     8 #include<vector>
     9 using namespace std;
    10 typedef long long LL;
    11 int val[7000];
    12 vector<int>vec[7000];
    13 int dp[2][7000];
    14 int cnt[7000];
    15 void dfs(int n);
    16 int main(void)
    17 {
    18     int n;
    19     while(scanf("%d",&n)!=EOF)
    20     {
    21         int i,j;
    22         memset(cnt,0,sizeof(cnt));
    23         for(i = 0; i < 7000; i++)
    24             vec[i].clear();
    25         memset(dp,0,sizeof(dp));
    26         for(i = 1; i <= n; i++)
    27             scanf("%d",&val[i]);
    28         int ch,fa;
    29         while(scanf("%d %d",&ch,&fa),ch!=0&&fa!=0)
    30         {
    31             cnt[ch] = 1;
    32             vec[fa].push_back(ch);
    33         }
    34         int root;
    35         for(i = 1; i <= n; i++)
    36         {
    37             if(!cnt[i])
    38             {
    39                 root = i;
    40                 break;
    41             }
    42         }
    43         dfs(root);
    44         if(dp[1][root]<0&&dp[0][root]<0)
    45         {
    46             printf("0
    ");
    47         }
    48         else
    49         {
    50             int ask = max(dp[1][root],dp[0][root]);
    51             printf("%d
    ",ask);
    52         }
    53     }
    54     return 0;
    55 }
    56 void dfs(int n)
    57 {
    58     int sum1 = 0;
    59     int sum2 = 0;
    60     for(int i = 0; i < vec[n].size(); i++)
    61     {
    62         int id = vec[n][i];
    63         dfs(id);
    64         sum1 += dp[0][id];
    65         sum2 += max(max(dp[1][id],dp[0][id]),0);
    66     }
    67     dp[0][n] = max(dp[0][n],sum2);
    68     dp[0][n] = max(dp[0][n],sum1);
    69     dp[1][n] = max(dp[0][n],val[n]);
    70     dp[1][n] = max(dp[0][n],sum1+val[n]);
    71 }
  • 相关阅读:
    判断是否是三角形,三角形面积,三角形内外切圆半径和面积
    输入从a加到b的两个数字
    九九乘法表
    某公司销售员工的年终奖根据该员工的年销售总额s提成,年销售总额超过1万元才提成,超过部分提成比例如下:
    判断是否是闰年?
    从键盘上输入三个点的坐标值(1,1)、(2,4)、(3,2),编程求该三角形的面积。
    输入一个正方形的边长,输出正方形的外接圆和内接圆的面积。
    .输入一个4位正整数,以相反的次序输出,例如,输入1234,输出为4321。
    SecoClient在win10系统中连接失败解决方案
    PHP 关于判断输入日期是否合法
  • 原文地址:https://www.cnblogs.com/zzuli2sjy/p/6236465.html
Copyright © 2020-2023  润新知