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

    Anniversary party
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 6062   Accepted: 3490

    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
    题意:某大学开一个晚会,有N个人,每个人都有自己的愉悦值,如果某人的上司在场那个人就会非常扫兴,所以上司和下属只能去一个,问怎样安排愉悦值最大。
    分析:dp[i][0] 代表第i个人不去能够获得的最大愉悦值,以i为根的子树能够获得的最大愉悦值
            dp[i][1] 代表第i个人去了能够获得的最大愉悦值,以i为根的子树能够获得的最大愉悦值
    dp[i][1] = dp[i][1] + dp[j][0] j为i的孩子
    dp[i][0] = dp[i][0] + max(dp[j][0],dp[j][1])
    由于这是单向边,找到根节点后进行DFS,然后选出 max(dp[root][0],dp[root][1])
    ///分析:dp[i][0] 代表第i个人不去能够获得的最大愉悦值,以i为根的子树能够获得的最大愉悦值
    ///      dp[i][1] 代表第i个人去了能够获得的最大愉悦值,以i为根的子树能够获得的最大愉悦值
    ///dp[i][1] = dp[i][1] + dp[j][0] j为i的孩子
    ///dp[i][0] = dp[i][0] + max(dp[j][0],dp[j][1])
    #include <iostream>
    #include <stdio.h>
    #include <string.h>
    #include <algorithm>
    #define N 6005
    using namespace std;

    int dp[N][2];
    int indgree[N];
    struct Edge{
        int u,v,next;
    }edge[N];
    int head[N];
    void addEdge(int u,int v,int &k){
        edge[k].u = u,edge[k].v = v;
        edge[k].next = head[u],head[u]=k++;
    }
    void dfs(int u){
        for(int k = head[u];k!=-1;k=edge[k].next){
            int v = edge[k].v;
            dfs(v);
            dp[u][0]+=max(dp[v][0],dp[v][1]);
            dp[u][1]+=dp[v][0];
        }
    }
    int main()
    {
        int n;
        while(scanf("%d",&n)!=EOF)
        {
            memset(head,-1,sizeof(head));
            memset(dp,0,sizeof(dp));
            memset(indgree,0,sizeof(indgree));
            for(int i=1;i<=n;i++){
                scanf("%d",&dp[i][1]);
            }
            int tot=0;
            int u,v;
            while(scanf("%d%d",&v,&u),u+v){
                addEdge(u,v,tot);
                indgree[v]++;
            }
            int root;
            for(int i=1;i<=n;i++){
                if(indgree[i]==0) root = i;
            }
            dfs(root);
            //printf("%d ",root);
            printf("%d ",max(dp[root][0],dp[root][1]));
        }
        return 0;
    }
  • 相关阅读:
    前缀和与差分
    可行!解决bitmap缩放失真问题
    Android 中的 File renameTo() 使用
    XMPP 中客户端断线及网络异常处理
    webview 离线缓存,html5游戏适用
    Mac os x下配置 Android ndk 开发环境
    从外企到国企的工作环境改变
    微软SQL Server数据库SQL语句导入导出大全,包括与其他数据库和文件的数据的导入导出
    人生第一职业:我当了人民教师
    JOIN 分为内连接,外连接(左外连接,右外连接,全外连接)
  • 原文地址:https://www.cnblogs.com/liyinggang/p/5410491.html
Copyright © 2011-2022 走看看