zoukankan      html  css  js  c++  java
  • Anniversary party

    Anniversary party

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


    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
     
     
    学校要开一个聚会。学校的教职工之间有上下级关系,为了让所有人开心,宴会组织者决定不会同时邀请一个人和他的上级,对于每一个人,他能给聚会带来的欢乐度有一个值,问组织者该邀请哪些人能够使宴会的欢乐度达到最大值。
    解题思路:
    首先是DP的部分(也是很无聊的一部分):每个参与者都有两种状态,一种是参加,一种是不参加。这个状态的后续影响就是如果他参加了,他的直接上司和直接下属都不能参加。我们可以用一个二维二态的数组来描述:dp[i][1]表示第i个参与者参加了,dp[i][0]表示第i个参与者没有参加。状态转移方程就是dp[i][1]=dp[i][1]+dp[i-1][0],dp[i][0]=dp[i][0]+Max(dp[i-1][0],dp[i-1][1])。
     
     树形dp题
     
     1 #include <bits/stdc++.h>
     2 
     3 #define N 100050
     4 using namespace std;
     5 int n;
     6 int dp[N][2];
     7 int an, fa;
     8 int f[N];
     9 int vis[N];
    10 vector<int> v[N];
    11 
    12 void dfs(int x){
    13     for(int i = 0 ; i < v[x].size(); i++){
    14         dfs(v[x][i]);
    15         dp[x][1] += dp[v[x][i]][0];
    16         dp[x][0] += max(dp[v[x][i]][0], dp[v[x][i]][1]);
    17     }
    18 }
    19 
    20 int main(){
    21     while(cin >> n){
    22         for(int i = 1; i <= n; i++)
    23             v[i].clear();
    24         memset(dp, 0,  sizeof(dp));
    25         for(int i = 1; i <= n; i++){
    26             cin >> dp[i][1];
    27         }
    28         memset(f, -1, sizeof(f));
    29         while(cin >> an >> fa && an && fa){
    30             v[fa].push_back(an);
    31             f[an] = fa;
    32         }
    33         int root = 1;
    34         while(f[root] != -1){
    35             root = f[root];
    36         }
    37         dfs(root);
    38         cout << max(dp[root][0], dp[root][1]) << endl;
    39     }
    40     return 0;
    41 }
  • 相关阅读:
    微信支付可能改变的六大行业
    WeChat Official Account Admin Platform API Introduction
    WeChat Official Account Admin Platform Message API Guide
    微信公众平台开发(61)预约挂号
    PHP获取Cookie模拟登录
    微信公众平台开发(60)每日英语
    微信公众平台开发(59)相册
    SQL语句执行顺序
    sql
    pthread_rwlock_t
  • 原文地址:https://www.cnblogs.com/zllwxm123/p/10651709.html
Copyright © 2011-2022 走看看