• Anniversary party HDU


    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

    题意:n个节点,每个节点对应一个值,对于一个节点u,如果选了他,就不能选他的儿子节点(直系子节点),但可以选他的孙子节点。给出n个节点的关系,求选若干个节点所得和的最大值。、
    输入:一个n,接下来n行的值代表每个节点的值,接下来若干行,每行两个值u,v;代表v是u的父节点。(当输入0 0时,输入结束)。

    状态:dp[u][0]代表不选节点u时的最大值;dp[u][1]代表选节点u时的最大值。
    转移方程:dp[u][1]+=dp[v][0]; dp[u][0]+=max(dp[v][0],dp[v][1]);
    (v指节点u的儿子节点,这里不会打求和的符号,只好写“+=”了。)
    实现:找到树根然后dfs从后先前推即可。
    (在此感谢某“狗”星人的推荐,阿里嘎多。推一下它的博客:https://www.cnblogs.com/1013star/p/9941532.html)

    #include <iostream>
    #include <cstdio>
    #include <cmath>
    #include <cstring>
    #include <algorithm>
    #include <vector>
    #include <queue>
    #include <stack>
    #define LL long long
    #define PI acos(-1.0)
    using namespace std;
    const int maxn=6e3+10;
    const int inf=0x3f3f3f3f;
    vector <int> ve[maxn];
    int in[maxn],arr[maxn];
    int dp[maxn][2],book[maxn];
    void dfs(int u)
    {
    
        book[u]=1;
        for(int i=0;i<ve[u].size();i++)
        {
            int v=ve[u][i];
            if(!book[v]) //该节点如果搜过了就跳过,没有必要再跑一遍。
            {
                dfs(v);
                dp[u][0]+=max(dp[v][0],dp[v][1]); //如果不选u节点,那么可以选其v,也可以不选v。
                dp[u][1]+=dp[v][0]; //如果选了u,那么他的儿子节点就都不能选了。
            }
    
        }
        return ;
    
    }
    int main(void)
    {
        int n;
        while(~scanf("%d",&n))
        {
            memset(in,0,sizeof(in));
            memset(book,0,sizeof(book));
            for(int i=1;i<=n;i++)
            {
                scanf("%d",&arr[i]);
                dp[i][0]=0;//不选初值肯定是0.
                dp[i][1]=arr[i];//选了初值就是本节点的值。
                ve[i].clear();//别忘初始化
            }
            int u,v;
            while(~scanf("%d%d",&u,&v))
            {
                if(u==0&&v==0) break;
                ve[v].push_back(u);
                in[u]++;
            }
            int root;
            //找根节点
            for(int i=1;i<=n;i++)
            {
                if(in[i]==0)
                {
                    root=i;
                    break;
                }
    
            }
            dfs(root);
            printf("%d
    ",max(dp[root][0],dp[root][1]));
    
        }
    
    	return 0;
    }
    
    
  • 相关阅读:
    典型用户和用户场景
    每日站立会议个人博客(冲刺周)-Tuesday
    WPS中VB编辑器的安装
    WARN hdfs.DataStreamer: Caught exception
    java.net.ConnectException: Your endpoint configuration is wrong; For more details see: http://wiki.apache.org/hadoop/UnsetHostnameOrPort
    “初始化 Java 工具”期间发生了内部错误, java.lang.NullPointerException
    在Github上搭建个人主页
    关于新版的MySQL安装教程
    stopping hbasecat:/tmp/hbase-root-master.pid:No such file or directory
    途牛订单的服务化演进
  • 原文地址:https://www.cnblogs.com/qinjames/p/10554690.html
Copyright © 2020-2023  润新知