zoukankan      html  css  js  c++  java
  • Anniversary party树形DP

    Anniversary party

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 3861    Accepted Submission(s): 1800


    Problem 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 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
    #include<iostream>
    #include<string>
    #define MAX 20000
    #define max(x,y) (x>y?x:y)
    using namespace std;
    struct node{
        int v;
        node *next;
    }*head[MAX],tree[MAX];
    int ptr, n, t;
    int dp[MAX][2], w[MAX];
    bool visit[MAX];
    void initial(){
        ptr = 1;
        memset(head, 0, sizeof(head));
        memset(visit, 0, sizeof(visit));
        memset(dp, 0, sizeof(dp));
    }
    void addEdge(int x, int y){
        tree[ptr].v = y; 
        tree[ptr].next = head[x];head[x] = &tree[ptr++];
        tree[ptr].v = x; 
        tree[ptr].next = head[y]; head[y] = &tree[ptr++];
    }
    void tree_DP(int v){
        if (visit[v])return;
        visit[v] = true;
        node *p = head[v];
        while (p){
            if (!visit[p->v]){
                tree_DP(p->v);
                dp[v][1] += dp[p->v][0];//如果选当前节点,就不选儿子节点
                dp[v][0] += max(dp[p->v][0], dp[p->v][1]);
            }
            p = p->next;
        }
        dp[v][1] += w[v];
    }
    int main()
    {
        int u, v;
        while (cin >> n){
            initial();
            for (int i = 1; i <= n; i++){
                cin >> w[i];
            }
            while (cin >> u >> v){
                if (u + v == 0)break;
                addEdge(u, v);
            }
            tree_DP(1);
            cout << max(dp[1][0], dp[1][1]) << endl;
        }
        return 0;
    }
    世上无难事,只要肯登攀。
  • 相关阅读:
    HashMap_经典存储_经典分拣思路
    (GOF23设计模式)_单例模式_应用场景_饿汉式_懒汉式
    sql简单测试
    Java概述
    搭建集群时的问题总结
    在Java连接hbase时出现的问题
    JavaAPI与hbase的交互
    Resourcemanager的高可用
    hbase安装
    hbase报错org.apache.hadoop.hbase.TableExistsException: hbase:namespace
  • 原文地址:https://www.cnblogs.com/littlehoom/p/3561861.html
Copyright © 2011-2022 走看看