HDU 2412 Party at Hali-Bula (树形DP + hash)

本博客探讨了一个组织者在邀请员工参加聚会时如何最大化邀请人数且避免员工与其上司同时被邀请的问题,并提供了算法解决方案及判断最优解是否唯一的方法。

Party at Hali-Bula

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


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

此题包含的问题有:

1)hash:这个直接用map就可以了,把字符串对应到int。

2)找出最多的人数:树形DP,dp[rt][0]表示不取rt节点时的人数,dp[rt][1]表示取了rt节点时的人数。从而有

dp[rt][0] = sum(max(dp[son][0],dp[son][1])); //不取当前点的最大值,从取孩子和不取孩子的最优值中取

dp[rt][1] = 1 + sum(dp[son][0]); //只能从不取孩子中取

答案就是max(dp[1][1],dp[1][0]);初始化dp为0.

3)判断最优解是否唯一:添加数组uni[][],uni[rt][0]=0表示dp[rt][0]的取法不唯一。uni[rt][1]表示dp[rt][1]的取法不唯一。那么转移方程为:

当dp[son][0] > dp[son][1]&&uni[son][0] == 0 或者 dp[son][0] < dp[son][1] && uni[son][1] == 0 或者 dp[son][0] == dp[son][1]时,有uni[rt][0] = 0;

当uni[son][0] ==0时,uni[rt][1] = 0;

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
#include <string>
#define SIZE 256

using namespace std;

struct node
{
    int to,next;
}edge[SIZE<<1];

int head[SIZE],idx;
int N,hash;
int dp[SIZE][2],uni[SIZE][2];
map <string,int> M;

void addnode(int from,int to)
{
    edge[idx].to = to;
    edge[idx].next = head[from];
    head[from] = idx++;
}

void dfs(int rt)
{
    bool flag = false;
    for(int i=head[rt]; i!=-1; i=edge[i].next)
    {
        flag = true;
        int to = edge[i].to;
        dfs(to);
        dp[rt][0] += max(dp[to][0],dp[to][1]);
        dp[rt][1] += dp[to][0];
        if((dp[to][0] > dp[to][1] && uni[to][0] == 0) ||
                (dp[to][1] > dp[to][0] && uni[to][1] == 0) ||
                (dp[to][0] == dp[to][1]))
            uni[rt][0] = 0;
        if(uni[to][0] == 0)
            uni[rt][1] = 0;
    }
    if(!flag)
    {
        dp[rt][0] = 0,dp[rt][1] = 1;
        uni[rt][0] = uni[rt][1] = 1;
    }
    else
        dp[rt][1] ++;
}

int main()
{
    while(~scanf("%d",&N) && N)
    {
        string s,e;
        hash = idx = 0;
        M.clear();
        memset(head,-1,sizeof(head));
        cin >> s;
        M[s] = ++hash;
        for(int i=1; i<N; i++)
        {
            cin >> s >> e;
            if(M[s] == 0)
                M[s] = ++hash;
            if(M[e] == 0)
                M[e] = ++hash;
            addnode(M[e],M[s]);
        }
        memset(dp,0,sizeof(dp));
        for(int i=1; i<=N; i++)
            uni[i][0] = uni[i][1] = 1;
        dfs(1);
        if(dp[1][0] > dp[1][1])
        {
            printf("%d ",dp[1][0]);
            puts(uni[1][0]?"Yes":"No");
        }
        else if(dp[1][0] < dp[1][1])
        {
            printf("%d ",dp[1][1]);
            puts(uni[1][1]?"Yes":"No");
        }
        else
        {
            printf("%d ",dp[1][1]);
            puts("No");
        }
    }
    return 0;
}


 

 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值