HDU 2412 Party at Hali-Bula 详解(基础树形DP+方案唯一)

12 篇文章 2 订阅


Party at Hali-Bula

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


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
 
思路:n个人形成一个关系树,每个节点代表一个人,节点的根表示这个人的唯一的直接上司,只有根没有上司。要求选取一部分人出来,使得每2个人之间不能有直接的上下级的关系,
求最多能选多少个人出来,并且求出获得最大人数的选人方案是否唯一。
前半部分很容易求得,直接一个树形DP,后面的判断最优解是否唯一比较难搞。。

新加一个状态
dup[i][j],表示相应的dp[i][j]是否是唯一方案。
§ 对于叶子结点, dup[k][0] = dup[k][1] = 1.
§ 对于非叶子结点,
对于i的任一儿子j,若(dp[j][0] > dp[j][1]  dup[j][0] == 0)  (dp[j][0] < dp[j][1]  dup[j][1] == 0)  (dp[j][0] == dp[j][1]),则dup[i][0] = 0
对于I的任一儿子jdup[j][0] = 0, 
dup[i][1] = 0
dup[i][1]=0表示dp[i][1]不唯一;dup[i][1]=1表示dp[i][1]唯一;
dup[i][0]同上。


这个题告诉了你每个人的名字,用到了一个比较经典的用法,用map把人名“映射到”数字上。。。这题直接告诉你了根节点。。
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <map>
#include <vector>
using namespace std;
const int maxn = 205;
int dp[maxn][3], book[maxn][3];
vector<int> v[maxn];
map<string, int> m;
char str[maxn], str1[maxn];
void dfs(int x)
{
    if(!v[x].size())
        book[x][1] = book[x][0] = 1;
    for(int i = 0; i < v[x].size(); i++)
    {
        int to = v[x][i];
        dfs(to);
        dp[x][1] += dp[to][0];
        dp[x][0] += max(dp[to][1], dp[to][0]);
        if((dp[to][1] > dp[to][0] && !book[to][1]) || (dp[to][0] > dp[to][1] && !book[to][0]) || (dp[to][0] == dp[to][1]))
            book[x][0] = 0;
        if(book[to][0] == 0)
            book[x][1] = 0;


    }
}
int main()
{
    int n, rt, len;
    while(~scanf("%d", &n),n)
    {
        m.clear();
        for(int i = 0; i <= n; i++)
            v[i].clear();
        memset(dp, 0, sizeof(dp));
        memset(book, 1, sizeof(book));
        rt = 1;
        len = 1;
        scanf("%s", str);
        m[str] = len++;
        for(int i = 1; i < n; i++)
        {
            scanf("%s%s", str, str1);
            if(!m[str]) m[str] = len++;
            if(!m[str1]) m[str1] = len++;
            v[m[str1]].push_back(m[str]);
        }
        for(int i = 1; i <= len; i++)
            dp[i][1] = 1;
        dfs(rt);
        if(dp[1][1] > dp[1][0])
        {
            if(book[1][1])
                printf("%d %s\n",dp[1][1], "Yes");
            else
                printf("%d %s\n",dp[1][1], "No");

        }
        else if(dp[1][0] > dp[1][1])
        {
            if(book[1][0])
                printf("%d %s\n",dp[1][0], "Yes");
            else
                printf("%d %s\n", dp[1][0], "No");
        }
        else
              printf("%d %s\n", dp[1][0], "No");
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值