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;
    }
    
    东北日出西边雨 道是无情却有情
  • 相关阅读:
    触达项目涉及到的功能点
    NodeJS编程基础
    C#Socket通讯
    HTML转义字符大全
    C# 二进制,十进制,十六进制 互转
    浏览器的分类
    Prometheus设置systemctl管理
    第十五讲:Pagerduty的联用
    第十四讲:Prometheus 企业级实际使⽤二
    第十三讲:Prometheus 企业级实际使⽤
  • 原文地址:https://www.cnblogs.com/ccut-ry/p/7546159.html
Copyright © 2011-2022 走看看