zoukankan      html  css  js  c++  java
  • POJ 2342 Anniversary party 【树形DP】

    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 N – 1 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

    题目大意:

    一个公司要举办晚宴,公司一共有n个人。每一个人在宴会上有一个幸福值,如果在宴会上碰到了自己的直接上司,那么他的幸福值就为0。所以考虑如何邀请人让宴会幸福值的和最大,输出最大的幸福值之和。

    大致思路:

    对于每一个人,都有来或者不来两种可能。dp[i][0/1] 代表第i个人来或者不来情况下前i个人的幸福值之和。

    利用树的性质递归求每一个dp [i]。最后取一个max。

    代码:

     1 #include<iostream>
     2 #include<cstdlib>
     3 #include<cstdio>
     4 #include<cstring>
     5 using namespace std;
     6 const int maxn=1e6+7;
     7 int father[maxn];
     8 int dp[maxn][2],n;
     9 bool visit[maxn];
    10 void tree_dp(int k)
    11 {
    12     visit[k]=false;
    13     for(int i=1;i<=n;++i){
    14         if(visit[i]&&father[i]==k){
    15             tree_dp(i);
    16             dp[k][1]+=dp[i][0];
    17             dp[k][0]+=max(dp[i][0],dp[i][1]);
    18         }
    19     }
    20 }
    21 int main()
    22 {
    23     ios::sync_with_stdio(false);
    24     //freopen("in.txt","r",stdin);
    25     int f,s,root=1;
    26     memset(father,0,sizeof(father));
    27     memset(dp,0,sizeof(dp));
    28     memset(visit,true,sizeof(visit));
    29     cin>>n;
    30     for(int i=1;i<=n;++i)
    31         cin>>dp[i][1];
    32 
    33     while(cin>>s>>f||(s&&f))
    34         father[s]=f;
    35     while(father[root])
    36         root=father[root];
    37     tree_dp(root);
    38     cout<<max(dp[root][0],dp[root][1])<<endl;
    39     return 0;
    40 }
  • 相关阅读:
    能组成多少个无重复数字且不为5的倍数的五位数有多少个?
    http与https
    观察者模式和发布/订阅模式的区别
    快速排序的最优时间复杂度是 O(nlogn)
    函数实现 composeFunctions(fn1,fn2,fn3,fn4)等价于fn4(fn3(fn2(fn1))
    vue双向绑定代码实现
    node历史版本下载
    阻止scroll冒泡
    中断或取消Promise链的可行方案
    从输入url到页面加载完成发生了什么?——前端角度
  • 原文地址:https://www.cnblogs.com/SCaryon/p/7381985.html
Copyright © 2011-2022 走看看