zoukankan      html  css  js  c++  java
  • hdu 1520 Anniversary party(树形DP)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1520

    Anniversary party

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


    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
     

    Recommend
    linle   |   We have carefully selected several similar problems for you:  1561 1011 2196 1494 2242 

    题目大意:n个结点。接下去n行分别表示第i个人具有的活跃值。再输入a,b表示b是a的上司。

    题目要求具有直接上司和下属关系的两个人不能同一时候參加party,终于输出让party活跃值最大是多少。

    解题思路:用dp[i][j]二维数组来计算。i来表示第几个人,j表示是去或者不去。

    dp[i][1]表示第i个參与者參加了,dp[i][0]表示第i个參与者没有參加。

    先得出所以子节点的去或者不去的最大值,通过子节点的值得到当前值。

    1、上司參加,直属下属就一定不能够參加。
    2、上司不參加。直属下属能够參加也能够不參加。



    详见代码。

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <vector>
    
    using namespace std;
    
    vector<int>V[6010];
    int dp[6010][10];
    
    void Dfs(int root)
    {
        int num=V[root].size();//求当前节点下有几个子节点
        for (int i=0; i<num; i++)
        {
            Dfs(V[root][i]);
        }
        for (int i=0; i<num; i++)
        {
            dp[root][0]+=max(dp[V[root][i]][0],dp[V[root][i]][1]);
            dp[root][1]+=dp[V[root][i]][0];
        }
    }
    
    int main()
    {
        int t,a,b;
        int val[6010];
        int fa[6010];
        while (~scanf("%d",&t))
        {
            memset(dp,0,sizeof(dp));
            for (int i=1; i<=t; i++)
            {
                scanf("%d",&val[i]);
                V[i].clear();
                fa[i]=i;
                dp[i][1]=val[i];
            }
            while (~scanf("%d%d",&a,&b))
            {
                if (a==0&&b==0)
                    break;
                fa[a]=b;
                V[b].push_back(a);
            }
            int root=1;
            while (fa[root]!=root)//找顶点
                root=fa[root];
            Dfs(root);
            int ans=max(dp[root][0],dp[root][1]);
            cout<<ans<<endl;
        }
        return 0;
    }
    </span>



  • 相关阅读:
    最长回文子串
    学习与提升
    jupyter notebook nginx 试听录音文件,网页试听
    【神经网络】batch、epoch、iteration的含义
    【tensorflow】Tensorflow入门教程
    最全BAT面试题
    打印二叉树
    基于argparser模块实现 ls 功能(基本实现)
    比较详细的argpasrse模块的基本使用
    ini文件转换为Json文件
  • 原文地址:https://www.cnblogs.com/yjbjingcha/p/7207866.html
Copyright © 2011-2022 走看看