zoukankan      html  css  js  c++  java
  • 树形dp

    poj 2342

    Anniversary party
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 9480   Accepted: 5454

    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 就需要你对递归有一个比较好的理解, 模拟建立一棵树, 找到树的根节点,采用 dfs 遍历一遍树,并且将走过的点做一个标记

    状态转移方程 :
      dp[node][1] += dp[i][0];
      dp[node][0] += max(dp[i][0], dp[i][1]);

    代码示例:
    int n;
    int val[6005];
    int pt[6005];
    vector<int>ve[6005];
    int dp[6005][2];
    
    void dfs(int x){
        
        for(int i = 0; i < ve[x].size(); i++){
            int to = ve[x][i];
            
            dfs(to);
            dp[x][1] += dp[to][0];
            dp[x][0] += max(dp[to][1], dp[to][0]);
        } 
    }
    
    int main() {
        //freopen("in.txt", "r", stdin);
        //freopen("out.txt", "w", stdout);
        int x, y;
        
        cin >> n;
        for(int i = 1; i <= n; i++) {
            scanf("%d", &val[i]);
            dp[i][1] = val[i];
        }
        for(int i = 1; i < n; i++){
            scanf("%d%d", &x, &y);
            ve[y].push_back(x);
            pt[x] = 1;        
        }    
        int root;
        for(int i = 1; i <= n; i++) if (!pt[i]) {root = i; break;}
        dfs(root);
        
        //printf("+++++ %d 
    ", dp[3][0]);
        printf("%d
    ", max(dp[root][0], dp[root][1]));
        return 0;
    }
    
    东北日出西边雨 道是无情却有情
  • 相关阅读:
    原来发布到主页是发布到博客园的主页啊,还真不知道
    sysobjects
    blend3破解文件,直接覆盖安装目录源文件即可。
    Microsoft Sql中char和varchar的区别与联系
    SQL脚本创建数据表,并给出主键,外键,唯一键,约束
    asp.net调试方法
    VS2010安装项目的系统必备中添加.NET 2.0
    c#,自定义安装,部署,创建自定义操作,自定义操作,安装验证,数据库,安装授权码,接收输入,判断
    Ext2.2系列(50篇)
    C# 获取24小时制的当前系统时间
  • 原文地址:https://www.cnblogs.com/ccut-ry/p/7546159.html
Copyright © 2011-2022 走看看