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

    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.
    InputEmployees 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 0OutputOutput 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
    题解:这题可能是多组数据,wa的想吐。。。可以看作是求树的最大独立集,固定套路。不过注意这颗树的边是有方向的,所以要找它的根,开始用并查集,后来发现其实用不上
     1 #include<vector>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<iostream>
     5 #include<algorithm> 
     6 using namespace std;
     7 
     8 const int maxn=6005;
     9 
    10 int n,ans;
    11 int Fa[maxn],dp[maxn][2];
    12 
    13 vector<int> G[maxn];
    14 
    15 void DFS(int pa,int u){
    16     for(int i=0;i<G[u].size();i++){
    17         int v=G[u][i];
    18         if(pa==v) continue;
    19         DFS(u,v);
    20         dp[u][0]+=max(dp[v][1],dp[v][0]);
    21         dp[u][1]+=dp[v][0];
    22     }
    23 }
    24 
    25 int main()
    26 {   while(~scanf("%d",&n)){
    27         for(int i=1;i<=n;i++){
    28             Fa[i]=0;
    29             dp[i][0]=0;
    30             G[i].clear();
    31         } 
    32         for(int i=1;i<=n;i++) scanf("%d",&dp[i][1]);
    33         for(int i=1;i<=n;i++){
    34             int u,v;
    35             scanf("%d%d",&u,&v);
    36             if(u==0&&v==0) break;
    37             Fa[u]=v;
    38             G[v].push_back(u);
    39         }
    40         int temp=n;
    41         while(Fa[temp]) temp=Fa[temp];
    42         
    43         DFS(0,temp);
    44         cout<<max(dp[temp][0],dp[temp][1])<<endl;
    45     }
    46 }
  • 相关阅读:
    2020Python作业——类与对象
    图文存储常识:单机、集中、分布式、云、云原生存储
    宜泊科技加入阿里云原生合作伙伴计划,共建智慧停车新生态
    dubbogo 3.0:牵手 gRPC 走向云原生时代
    千万商家的智能决策引擎AnalyticDB如何助力生意参谋双十一
    我在阿里云做前端代码智能化
    网络病毒源的排查(2005年3月22日维护记录)
    在页面中控制媒体流的起播点和播放长度
    注意服务器系统日期对防病毒软件的影响
    修改SQL SERVER虚拟服务器IP的问题
  • 原文地址:https://www.cnblogs.com/zgglj-com/p/7748287.html
Copyright © 2011-2022 走看看