zoukankan      html  css  js  c++  java
  • 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个人,分别给出了每个人的价值,他们要被邀请去参加聚会,但是每个人不能和自己的直接领导同时被邀请,问邀请后实现的最大价值是多少

    【思路】dp[k][1]+=dp[i][0]表示k是i的领导,1表示去,0表示不去,领导k去了,i就不去了;

    dp[k][0]+=max(dp[i][0],dp[i][1]);领导k不去,i可去可不去,取大值

    #include<iostream>
    #include<stdio.h>
    #include<string.h>
    using namespace std;
    const int N=6005;
    int n;
    int fa[N],vis[N],dp[N][2];
    void dfs(int k)
    {
        vis[k]=1;
        for(int i=1; i<=n; i++)
        {
            if(!vis[i]&&fa[i]==k)
            {
                dfs(i);
                dp[k][1]+=dp[i][0];
                dp[k][0]+=max(dp[i][0],dp[i][1]);
            }
        }
    }
    int main()
    {
        while(~scanf("%d",&n))
        {
    
            memset(fa,0,sizeof(fa));
            for(int i=1; i<=n; i++)
            {
                scanf("%d",&dp[i][1]);
            }
            int k=0;
            int a,b;
            while(scanf("%d%d",&a,&b))
            {
    
                if(a==0&&b==0) break;
                fa[a]=b;
                k=b;
            }
            memset(vis,0,sizeof(vis));
            dfs(k);
            printf("%d
    ",max(dp[k][0],dp[k][1]));
        }
        return 0;
    }
  • 相关阅读:
    学习vim命令:“:w !sudo tee %”
    mac下安装和卸载软件
    很好用的在线markdown编辑器
    doc2vec 利用gensim 生成文档向量
    C语言经典算法100例-024-求数列的前20 项和,2/1,3/2,5/3,8/5...
    C语言经典算法100例-023-打印菱形
    C语言经典算法100例-022-乒乓球比赛名单问题
    C语言经典算法100例-021-猴子吃桃问题
    C语言经典算法100例-020-小球自由下落问题
    C语言经典算法100例-019-求完数
  • 原文地址:https://www.cnblogs.com/iwantstrong/p/5842697.html
Copyright © 2011-2022 走看看