HDU2412 && POJ3342:Party at Hali-Bula(树形DP)

来源:互联网 发布:不知其可也的知意思 编辑:程序博客网 时间:2024/05/16 19:13

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 integern (1 ≤n ≤ 200), the number of BCM employees. The next line contains the name of the Big Boss only. Each of the followingn-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

6JasonJack JasonJoe JackJill JasonJohn JackJim Jill2MingCho Ming0

Sample Output

4 Yes1 No
 
题意:与POJ2342类似,也是求在上司与下属不同时出现的情况下,最多出现的人数,并且方案唯一输出Yes,不唯一输出NO
思路:树形DP
#include <stdio.h>#include <string.h>#include <algorithm>using namespace std;struct node{    int now,next;} tree[205];char name[205][30];int head[205],res[205],dp[205][2],len,cnt;int find(char *str)//找到str在name中的位置,没有则放到name尾{    int i;    for(i = 1; i<len; i++)    {        if(!strcmp(name[i],str))            return i;    }    strcpy(name[len],str);    return len++;}int add(int x,int y)//加入树中{    tree[cnt].now = y;    tree[cnt].next = head[x];    return cnt++;}int dfs(int root)//深搜找到答案{    int i,j,sum1 = 0,sum2 = 0;    if(!head[root])    {        res[root] = 1;        dp[root][1] = 1;        dp[root][0] = 0;        return res[root];    }    for(i = head[root]; i; i = tree[i].next)    {        sum1+=dfs(tree[i].now);        for(j = head[tree[i].now]; j; j = tree[j].next)            sum2+=res[tree[j].now];    }    dp[root][1] = sum1;//root去    dp[root][0] = sum2+1;//不去    res[root] = max(dp[root][0],dp[root][1]);    return res[root];}int check(int root)//判断方案唯一性{    int i,j;    if(!head[root])//BOSS之下无叶子节点,必然只有一种        return 1;    if(dp[root][1] == dp[root][0])//root去不去都是一样,证明不只一种        return 0;    if(dp[root][1] > dp[root][0])//去的方案总人数比不去的多    {        for(i = head[root]; i; i = tree[i].next)            if(!check(tree[i].now))//一直往下搜到如果存在不唯一的方案,即不唯一                return 0;    }    else    {        for(i = head[root]; i; i = tree[i].next)//root不去的方案        {            for(j = head[tree[i].now]; j; j = tree[j].next)                if(!check(tree[j].now))                    return 0;        }    }    return 1;//所有方案都没有多种,即只有一种方案}int main(){    int n,i,j,x,y,pos;    char str1[20],str2[20];    while(scanf("%d",&n),n)    {        len = cnt = 1;        memset(head,0,sizeof(head));        memset(dp,0,sizeof(dp));        scanf("%s",str1);        pos = find(str1);        for(i = 1; i<n; i++)        {            scanf("%s%s",str1,str2);            x = find(str1);            y = find(str2);            head[y] = add(y,x);//y的子节点为x        }        printf("%d ",dfs(1));//从树的顶点开始往下搜索        if(check(1))            printf("Yes\n");        else            printf("No\n");    }    return 0;}

原创粉丝点击