zoukankan      html  css  js  c++  java
  • ural 1039 Anniversary Party

    1039. Anniversary Party

    Time limit: 0.5 second
    Memory limit: 8 MB

    Background

    The president of the Ural State University is going to make an 80'th Anniversary party. The university has a hierarchical structure of employees; that is, the supervisor relation forms a tree rooted at the president. Employees are numbered by integer numbers in a range from 1 to N, The personnel office has ranked each employee with a conviviality rating. In order to make the party fun for all attendees, the president does not want both an employee and his or her immediate supervisor to attend.

    Problem

    Your task is to make up a guest list with the maximal conviviality rating of the guests.

    Input

    The first line of the input contains a number N. 1 ≤ N ≤ 6000. 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 the supervisor relation tree goes. Each line of the tree specification has the form
    <L> <K>
    
    which means that the K-th employee is an immediate supervisor of L-th employee. Input is ended with the line
    0 0
    

    Output

    The output should contain the maximal total rating of the guests.

    Sample

    inputoutput
    7
    1
    1
    1
    1
    1
    1
    1
    1 3
    2 3
    6 4
    7 4
    4 5
    3 5
    0 0
    
    5

    没有上司的年会。ural要举办场年会,请你邀请来宾。要求每位客人的直接上司不被邀请,否则会很尴尬。每位客人有一个兴趣值。要求使来宾的兴趣值之和最大。

    每个人可以选择去或者不去。所以:

    dp[t][0] 表示以t为最高上司的子树不安排t参加的最大兴趣值。

    dp[t][1] 表示以t为最高上司的子树安排t参加的最大兴趣值。

    不安排t参加的话,其直系下属可以去或者不去。dp[t][0] = sum(max(dp[ti][0], dp[ti][1]));

    安排t参加的话,其直系下属均不能参加。dp[t][1] = sum(dp[ti][0]) + c[t];

    #include <cstdio>
    #include <vector>
    #include <cstring>
    #include <iostream>
    using namespace std;
    
    int n;
    int c[6006];
    std::vector<int > v[6006];
    int dp[6006][2];
    
    void Tdp(int t) {
        if(v[t].size() == 0) {
            dp[t][0] = 0;
            dp[t][1] = c[t];
            return ;
        }
        for (int i=0; i<v[t].size(); i++) {
            Tdp(v[t][i]);
        }
        for (int i=0; i<v[t].size(); i++) {
            dp[t][0] += max(dp[v[t][i]][0], dp[v[t][i]][1]);
            dp[t][1] += dp[v[t][i]][0];
        }
        dp[t][1] += c[t];
    }
    
    int main () {
        cin >> n;
        for (int i=1; i<=n; i++) {
            cin >> c[i] ;
        }
        int l ,k;
        int vis[6006];
        memset(vis, false, sizeof(vis));
        while (cin >> l >> k) {
            if (l == 0 && k == 0) break;
            v[k].push_back(l);
            vis[l] = true;
        }
        int root;
        for (int i=1; i<=n; i++) {
            if (!vis[i]) {
                root = i;//根节点(最高上司)为没有父节点的点。
                break;
            }
        }
        Tdp(root);
        cout << max(dp[root][0], dp[root][1]) <<endl;
        return 0;
    }
  • 相关阅读:
    一个很实用的css3兼容工具很多属性可以兼容到IE6
    html5 canvas 填充渐变形状
    找到任何形状的中心-总结篇
    html canvas非正方旋转和缩放...写的大多是正方的有人表示一直看正方的看厌了
    把jQuery的类、插件封装成seajs的模块的方法
    那些年实用但被我忘掉javascript属性.onresize
    总有一些实用javascript的元素被人遗忘在角落-slice
    jquery(入门篇)无缝滚动
    html5 canvas旋转+缩放
    今天看到这篇新闻之后,决定休息一下咯
  • 原文地址:https://www.cnblogs.com/xuelanghu/p/4276194.html
Copyright © 2011-2022 走看看