zoukankan      html  css  js  c++  java
  • hdu 2412 Party at Hali-Bula 经典树形DP

    Party at Hali-Bula

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 1365    Accepted Submission(s): 454


    Problem Description
    Dear Contestant,

    I'm going to have a party at my villa at Hali-Bula to celebrate my retirement from BCM. I wish I could invite all my co-workers, but imagine how an employee can enjoy a party when he finds his boss among the guests! So, I decide not to invite both an employee and his/her boss. The organizational hierarchy at BCM is such that nobody has more than one boss, and there is one and only one employee with no boss at all (the Big Boss)! Can I ask you to please write a program to determine the maximum number of guests so that no employee is invited when his/her boss is invited too? I've attached the list of employees and the organizational hierarchy of BCM.

    Best,
    --Brian Bennett

    P.S. I would be very grateful if your program can indicate whether the list of people is uniquely determined if I choose to invite the maximum number of guests with that condition.
     
    Input
    The input consists of multiple test cases. Each test case is started with a line containing an integer n (1 ≤ n ≤ 200), the number of BCM employees. The next line contains the name of the Big Boss only. Each of the following n-1 lines contains the name of an employee together with the name of his/her boss. All names are strings of at least one and at most 100 letters and are separated by blanks. The last line of each test case contains a single 0.
     
    Output
    For each test case, write a single line containing a number indicating the maximum number of guests that can be invited according to the required condition, and a word Yes or No, depending on whether the list of guests is unique in that case.
     
    Sample Input
    6
    Jason
    Jack Jason
    Joe Jack
    Jill Jason
    John Jack
    Jim Jill
    2
    Ming
    Cho Ming
    0
    Sample Output
    4 Yes
    1 No
     
    Source
     
    题意:有个人想请客,但是由于要请的人之间存在上司和下属的关系,为了能避免下属和直接上司一起,寻找一个合理的方案。
             求最大的请客人数,上司和下属满足一棵树。
     
    思路:如何处理字符串,转化成一棵树? 由于根节点已经知道,所以通过统计i点有多少个子节点就可以了。
            通过搜索来求取,关键是状态转移怎么写。
       dp[ i ] [ 0 ] 代表不包含第 i 个节点时的 最大人数目。
       dp[ i ] [ 1 ] 代表   包含第 i 个节点时到 最大人数目。
            那么对于叶子节点
            dp[ i ] [ 0 ]=0; dp[ i ] [ 1]=1;
            对于非叶子节点,
            dp[ i ] [ 0 ]= sum{   Max(dp[ j ] [ 0 ], dp[ j ] [ 1 ]  }; 其中dp[ j ] [ 0 ]的 j 是 i 的子节点。
            dp[ i ] [ 1 ]= sum{   dp[ j ] [ 0 ] } + 1 ;
      //画图试一试就知道了。
     
            对于统计是否唯一,另设一个数组ndp[ i ] [ j ]
        对于叶子节点:
              ndp[ i ] [ 0 ]=1; 代表不包含第 i 点的时候,全部子节点的种类是 唯一 的。
              ndp[ i ] [ 0 ]=0; 代表不包含第 i 点的时候,全部子节的的种类是  不唯一的。
     
              ndp[ i ] [ 1 ]=0; 代表  包含第  i 点的时候,全部子节点的种类是  不唯一。
              ndp[ i ] [ 1 ]=1;反之。
     
            非叶子节点:
        1.( dp[ k ] [ 0 ]> dp[ k ] [ 1 ] && ndp[ k ][ 0 ]==0)
            2.( dp[ k ] [ 1 ]> dp[ k ] [ 0 ] && ndp[ k ][ 1 ]==0)
            3.dp[ k ] [ 1 ] = = dp[ k ] [ 0 ];
           1或2或3满足 ,则 ndp[ k ] [ 0 ]= 0; why???要看看dp[ K ] [ 0 ];就知道了。
     
            如果存在子节点 满足 ndp[ j ] [ 0 ]==0 则 ndp[ k ] [ 0 ]=0; //j代表是 K 的子节点。
     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<cstdlib>
     5 using namespace std;
     6 
     7 struct node
     8 {
     9     int next[202];
    10     int num;
    11 }f[202];
    12 char cur[202][103];
    13 int len;
    14 bool glag,flag;
    15 int dp[202][2];
    16 int ndp[202][2];
    17 
    18 int Max(int x,int y)
    19 {
    20     return x>y? x:y;
    21 }
    22 
    23 int serch(char a[])
    24 {
    25     int i;
    26     for(i=1;i<=len;i++)
    27     {
    28         if(strcmp(a,cur[i])==0)
    29         return i;
    30     }
    31     strcpy(cur[++len],a);
    32     return len;
    33 }
    34 
    35 void dfs(int k)
    36 {
    37     int i,q,cur=0;
    38     if(f[k].num==0)
    39     {
    40         dp[k][0]=0;
    41         dp[k][1]=1;
    42         ndp[k][1]=1;
    43         ndp[k][0]=1;
    44         return;
    45     }
    46     for(i=1;i<=f[k].num;i++)
    47     {
    48         q=f[k].next[i];
    49         dfs(q);
    50         cur=Max(dp[q][0],dp[q][1]);
    51         dp[k][0]+=cur;
    52         dp[k][1]+=dp[q][0];
    53 
    54         if(dp[q][0]>dp[q][1]&&ndp[q][0]==0)
    55         ndp[k][0]=0;
    56         if(dp[q][1]>dp[q][0]&&ndp[q][1]==0)
    57         ndp[k][0]=0;
    58         if(dp[q][0]==dp[q][1]) ndp[k][0]=0;
    59 
    60         if(ndp[q][0]==0) ndp[k][1]=0;
    61     }
    62     dp[k][1]++;
    63     if(ndp[k][0]==-1) ndp[k][0]=1;
    64     if(ndp[k][1]==-1) ndp[k][1]=1;
    65 }
    66 
    67 int main()
    68 {
    69     int i,n,ans1,ans2;
    70     char a[105],b[105];
    71     while(scanf("%d",&n)>0)
    72     {
    73         if(n==0)break;
    74         for(i=1;i<=200;i++) f[i].num=0;
    75         scanf("%s",cur[1]);
    76         len=1;
    77         for(i=1;i<n;i++)
    78         {
    79             scanf("%s%s",a,b);
    80             ans1=serch(a);
    81             ans2=serch(b);
    82 
    83             f[ans2].num++;
    84             f[ans2].next[f[ans2].num]=ans1;
    85 
    86         }
    87         memset(dp,0,sizeof(dp));
    88         memset(ndp,-1,sizeof(ndp));
    89         glag=false;flag=false;
    90         dfs(1);
    91 
    92         if(dp[1][0]>dp[1][1]&&ndp[1][0]==1)
    93         printf("%d Yes
    ",dp[1][0]);
    94         else if(dp[1][1]>dp[1][0]&&ndp[1][1]==1)
    95         printf("%d Yes
    ",dp[1][1]);
    96         else printf("%d No
    ",Max(dp[1][0],dp[1][1]));
    97     }
    98     return 0;
    99 }
  • 相关阅读:
    数据结构实践——败者树归并模拟
    systemctl介绍
    Andriod Atom x86模拟器启动报错。
    (白书训练计划)UVa 11572 Unique Snowflakes(窗体滑动法)
    OpenCV2.3.1在CentOS6.5下的安装
    本学期课程教学要解决这个问题要点备忘录
    firefox浏览器和IE
    [LeetCode] Validate Binary Search Tree
    android性能优化优秀文章
    如何在使用eclipse的情况下,清理android项目中的冗余class文件和资源文件以及冗余图片
  • 原文地址:https://www.cnblogs.com/tom987690183/p/3393247.html
Copyright © 2011-2022 走看看