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

    题目链接:

    Anniversary party

    Time Limit: 2000/1000 MS (Java/Others)    

    Memory Limit: 65536/32768 K (Java/Others)


    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
     
    题意:
     

    给每个点的权值,和其中一些点的父子关系,父节点和子节点不能同时出现,问sum最大是多少;
     
    思路:
     
    树形dp的模板题,感觉第一次写树形dp也是跟dp差不多,只不过跟树有关系,还有题目中的权值可能是负的,我感觉可能会这种wa,没想到还AC了;
     
     
    AC代码:
     
    /*    1520    124MS    1988K    1430 B    G++    2014300227*/
    #include <bits/stdc++.h>
    using namespace std;
    const int N=6e3+4;
    typedef long long ll;
    const double PI=acos(-1.0);
    int n,a[N],vis[N],dp[N][2],fa[N];
    vector<int>ve[N];
    void dfs(int x)
    {
        vis[x]=1;
        int len=ve[x].size();
        if(len==0)
        {
            dp[x][0]=0;
            dp[x][1]=max(0,a[x]);
            return ;
        }
        else {
        int sum1=0,sum2=0;
        for(int i=0;i<len;i++)
        {
            int y=ve[x][i];
            if(!vis[y])dfs(y);//加个判断可以避免重复迭代;
            sum1+=max(dp[y][0],dp[y][1]);
            sum2+=dp[y][0];
        }
        dp[x][0]=sum1;
        dp[x][1]=sum2+max(0,a[x]);
        }
    }
    int main()
    {
        while(scanf("%d",&n)!=EOF)
        {
            for(int i=1;i<=n;i++)
            {
                fa[i]=-1;
                vis[i]=0;
                dp[i][0]=dp[i][1]=0;
                ve[i].clear();
                scanf("%d",&a[i]);
            }
            int u,v;
            while(1)
            {
                scanf("%d%d",&u,&v);
                if(u==0&&v==0)break;
                fa[u]=v;
                ve[v].push_back(u);
            }
            int ans=0;
            for(int i=1;i<=n;i++)
            {
                if(!vis[i])
                {
                    dfs(i);
                }
            }
            for(int i=1;i<=n;i++)
            {
                if(fa[i]==-1)
                {
                    int mmax=max(dp[i][1],dp[i][0]);
                    if(mmax>0)ans+=mmax;
                }
    
            }
            printf("%d
    ",ans);
    
        }
        return 0;
    }
  • 相关阅读:
    windows系统往远程桌面上共享文件(某磁盘下文件)如何远程连接传输文件。
    小程序实现读数据、统计词频、建词典
    pickle模块以特殊的二进制格式保存和恢复数据对象
    用一个简单小程序谈import和from...import的区别
    windows系统(64bit)安装python、pytorch
    SQL Server 一个简单的游标
    SQL server高级语法
    SQL server基本语法
    SSIS SQL Server配置自动作业
    Power BI 入门资料
  • 原文地址:https://www.cnblogs.com/zhangchengc919/p/5379946.html
Copyright © 2011-2022 走看看