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

input output
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;
}
时间: 2024-08-06 20:06:13

ural 1039 Anniversary Party的相关文章

树形DP URAL 1039 Anniversary Party

题目传送门 1 /* 2 题意:上司在,员工不在,反之不一定.每一个人有一个权值,问权值和最大多少. 3 树形DP:把上司和员工的关系看成根节点和子节点的关系,两者有状态转移方程: 4 dp[rt][0] += max (dp[son][1], dp[son][0]); //上司不去 5 dp[rt][1] += dp[son][0]; //上司去,员工都不去 6 */ 7 #include <cstdio> 8 #include <cstring> 9 #include <

URAL 1039 Anniversary Party 树形DP 水题

1039. Anniversary Party Time limit: 0.5 secondMemory 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 rela

ural 1039 树dp

http://acm.timus.ru/problem.aspx?space=1&num=1039 1039. Anniversary Party Time limit: 0.5 secondMemory 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 st

URAL 1776 Anniversary Firework 概率dp+区间dp

A - Anniversary Firework Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice URAL 1776 Appoint description:  System Crawler  (2016-05-06) Description Denis has to prepare the Ural State University 90th

HDU1520——树形DP——Anniversary party

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.

Anniversary party

Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9354   Accepted: 5400 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

poj2342 Anniversary party 简单树形dp

http://poj.org/problem?id=2342 Anniversary party Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4322   Accepted: 2459 Description There is going to be a party to celebrate the 80-th Anniversary of the Ural State University. The Universi

HDOJ 题目1520 Anniversary party(树形dp)

Anniversary party Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6271    Accepted Submission(s): 2820 Problem Description There is going to be a party to celebrate the 80-th Anniversary of the

HDU-1520 Anniversary party (树形DP)

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.