hdu 2412 Party at Hali-Bula

Party at Hali-Bula

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


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
 

树形dp,每一个人仅仅有一个领导,big boss没有领导,领导和员工不能同一时候在场。

dp[f][1]+=dp[v][0];        若dp[v][0]选择不为一。则dp[f][1]也不唯一。

dp[f][0]+=max(dp[v][1],dp[v][0]);    同上。另外,若dp[v][1],dp[v][0]相等。则也不唯一。

#include"stdio.h"
#include"string.h"
#define N 205
#define M 105
int k,cnt,head[N],mark[N];
char str[N][M];
struct node
{
    int v,next;
}e[N];
struct st
{
    int s,flag;
}dp[N][2];
int find(char*s)              //查找该点是否出现过
{
    int i;
    for(i=0;i<k;i++)
    {
        if(strcmp(str[i],s)==0)
            break;
    }
    if(i==k)
        strcpy(str[k++],s);
    return i;
}
void add(int u,int v)         //存有向边关系,u指向v
{
    e[cnt].v=v;
    e[cnt].next=head[u];
    head[u]=cnt++;
}
void dfs(int f)
{
    int u=head[f];
    mark[f]=1;
    dp[f][1].s=1;
    while(u!=-1)
    {
        int v=e[u].v;
        if(!mark[v])
        {
            dfs(v);
            dp[f][1].s+=dp[v][0].s;
            if(dp[v][0].flag==1)
                dp[f][1].flag=1;
            if(dp[v][0].s>dp[v][1].s)
            {
                dp[f][0].s+=dp[v][0].s;
                if(dp[v][0].flag==1)
                    dp[f][0].flag=1;
            }
            else if(dp[v][0].s<dp[v][1].s)
            {
                dp[f][0].s+=dp[v][1].s;
                if(dp[v][1].flag==1)
                    dp[f][0].flag=1;
            }
            else
            {
                dp[f][0].s+=dp[v][1].s;
                dp[f][0].flag=1;
            }
        }
        u=e[u].next;
    }
}
int main()
{
    int i,n;
    char s1[M],s2[M];
    while(scanf("%d",&n),n)
    {
        k=cnt=0;
        memset(head,-1,sizeof(head));
        scanf("%s",str[k++]);
        for(i=1;i<n;i++)
        {
            scanf("%s %s",s1,s2);
            int u,v;
            u=find(s2);
            v=find(s1);
            add(u,v);
        }
        memset(mark,0,sizeof(mark));
        memset(dp,0,sizeof(dp));
        dfs(0);
        int ans,flag=0;
        if(dp[0][0].s>dp[0][1].s)
        {
            ans=dp[0][0].s;
            flag=dp[0][0].flag;
        }
        else if(dp[0][0].s<dp[0][1].s)
        {
            ans=dp[0][1].s;
            flag=dp[0][1].flag;
        }
        else
        {
            ans=dp[0][0].s;
            flag=1;
        }
        printf("%d ",ans);
        if(flag)
            printf("No
");
        else
            printf("Yes
");
    }
    return 0;
}



原文地址:https://www.cnblogs.com/liguangsunls/p/6927650.html