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;
    }
    世上无难事,只要肯登攀。
  • 相关阅读:
    Jmeter测试报表相关参数说明
    jQuery实现图片延迟加载
    Jmeter 用动态参数进行测试
    MyEclipse 10.6 汉化和破解工具
    将图片转成二进制并生成Base64编码,可以在网页中通过url查看图片
    List的二种循环速度比较
    java+flash在线拍照和编辑,保存到服务器(Spring3.2.2+swf+jquery)
    eclipse中如何去除警告:Class is a raw type. References to generic type Class<T> should be parameterized
    weblogic异常weblogic.utils.NestedRuntimeException: Cannot parse POST parameters of request
    在静态页面html中跳转传值
  • 原文地址:https://www.cnblogs.com/littlehoom/p/3561861.html
Copyright © 2011-2022 走看看