zoukankan      html  css  js  c++  java
  • poj 2342 Anniversary party   (树形DP)

    地址 http://poj.org/problem?id=2342

    Anniversary party
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 8096   Accepted: 4631

    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

    Source

     
    分析:树形DP的模版题
    首先要找到根节点,也就是职位最高的人,然后从根节点进行DP
    dp[x][1]代表x到场的,以x为根节点的总体的愉悦度。
    dp[x][0]代表x未到场
    状态转移方程
    do[x][1]+=dp[i][0]  其中x为i的上司,由题意故i不能到场
     dp[x][0]+=max(dp[i][1],dp[i][0]);
     
    代码如下:
    #include <cstdio>
    #include <algorithm>
    #include <cstring>
    using namespace std;
    int dp[6200][2];
    int pre[6200];
    int vis[6200];
        int n,root,a,b,maxx;
    void dfs(int x)
    {
        vis[x]=1;
        for(int i=1;i<=n;i++)
        {
            if(pre[i]==x&&!vis[i])
            {
                dfs(i);
                dp[x][1]+=dp[i][0];
                dp[x][0]+=max(dp[i][1],dp[i][0]);
            }
        }
    }
    int main()
    {
        while(scanf("%d",&n)!=EOF)
        {
            memset(vis,0,sizeof(vis));
            memset(dp,0,sizeof(dp));
            memset(pre,0,sizeof(pre));
            for(int i=1;i<=n;i++)
            {
            pre[i]=i;
            scanf("%d",&dp[i][1]);
            }
            while(scanf("%d%d",&a,&b)!=EOF)
            {
                if(a==0&&b==0) break;
                pre[a]=b;
            }
            root=1;
            while(pre[root]!=root)
              root=pre[root];
              dfs(root);
              maxx=max(dp[root][1],dp[root][0]);
              printf("%d
    ",maxx);
        }
        return 0;
    } 
     
  • 相关阅读:
    关于VSCode如何缩进两个空格
    基于vue来开发一个仿饿了么的外卖商城(二)
    在复杂的项目开发中使用结对编程
    第一个博客
    docker容器启动失败解决办法
    windows转mac-开发环境搭建(六):mac上搭建git环境
    windows转mac-开发环境搭建(五):mac上用docker安装并运行mysql
    windows转mac-开发环境搭建(四):mac上搭建node、VS code、idea环境
    windows转mac-开发环境搭建(三):mac上搭建maven环境
    windows转mac-开发环境搭建(二):mac上搭建java环境
  • 原文地址:https://www.cnblogs.com/a249189046/p/6675353.html
Copyright © 2011-2022 走看看