zoukankan      html  css  js  c++  java
  • poj2342 Anniversary party

    Anniversary party
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 8028   Accepted: 4594

    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 N – 1 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

    Source

    解题思路:
    第一个树形dp的题、、、
    思路还是比较好理解的。
    每个人有来与不来两种状态,当来的时候,他的直接下属就不能来。----①
    当他不来的时候,他的直接下属可以来,可以不来。----②
    用dp[i][1]表示编号为i的人的时候,以它为根的子树所以产生的活跃值:由①可知dp[i][1]=dp[i][1]+dp[儿子节点][0];
    用dp[i][0]表示编号为i的人不来的时候,以它为根的子树所产生的活跃值:由②可知dp[i][0]=dp[i][1]+max(dp[儿子节点][0],dp[儿子节点][1]);
     
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <vector>
     4 #include <queue>
     5 #include <map>
     6 #include <cmath>
     7 #include <stack>
     8 #include <cstring>
     9 #include <algorithm>
    10 #include <cstdlib>
    11 #define FOR(i,x,n) for(long i=x;i<n;i++)
    12 #define ll long long int
    13 #define INF 0x3f3f3f3f
    14 #define MOD 1000000007
    15 #define MAX_N 60
    16 #define MAX_M 1005
    17 
    18 using namespace std;
    19 
    20 struct node{
    21     int number;
    22     int rating;
    23     int sonNum;
    24     int son[500];
    25     int father;
    26 };
    27 node tree[6005];
    28 //int visable[6005];
    29 int dp[6005][2];//0表示不去,1表示去
    30 
    31 void dfs(int root){
    32     FOR(i,0,tree[root].sonNum){
    33         dfs(tree[root].son[i]);
    34         dp[root][0]+=max(dp[tree[root].son[i]][0],dp[tree[root].son[i]][1]);
    35         dp[root][1]+=dp[tree[root].son[i]][0];
    36     }
    37 }
    38 
    39 int main()
    40 {
    41     //freopen("input1.txt", "r", stdin);
    42     //freopen("data.out", "w", stdout);
    43     int n;
    44     //memset(visable,0,sizeof(visable));
    45     memset(dp,0,sizeof(dp));
    46     scanf("%d",&n);
    47     FOR(i,1,n+1){
    48         tree[i].father=i;
    49         tree[i].sonNum=0;
    50     }
    51     FOR(i,1,n+1){
    52         scanf("%d",&dp[i][1]);
    53     }
    54     int t1,t2;
    55     FOR(i,0,n){
    56         scanf("%d %d",&t1,&t2);
    57         if(!(t1+t2)){
    58             break;
    59         }
    60         tree[t1].father=t2;
    61         tree[t2].son[tree[t2].sonNum++]=t1;
    62     }
    63     int t=1;
    64     while(tree[t].father!=t){
    65         t=tree[t].father;
    66     }
    67     dfs(t);
    68     printf("%d",max(dp[t][0],dp[t][1]));
    69     //fclose(stdin);
    70     //fclose(stdout);
    71     return 0;
    72 }
    View Code
  • 相关阅读:
    LeetCode 25. Reverse Nodes in k-Group
    LeetCode 66. Plus One
    LeetCode 69. Sqrt(x)
    很认真的聊一聊程序员的自我修养
    LeetCode 24. Swap Nodes in Pairs
    unordered_map和map的区别
    子查询一定要注意,别忘记加TOP 1,不然就GG了,过了好久测试给我测出来了
    Tree 通过父id找所有子节点
    SqlSugar CURD
    什么是.NET Framwork
  • 原文地址:https://www.cnblogs.com/TWS-YIFEI/p/6653010.html
Copyright © 2011-2022 走看看