Party at Hali-Bula(树形dp)

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个人,有上司个员工关系,上司来了,员工就不能来,员工来了,上司就不来,问最多邀请多少人,并且最多人数的方案是否唯一。

典型的树形dp,dp[0][i]表示不选第i个人,dp[1][i]表示选第i个人。
1,如果选第i个人,那么dp[1][j] = sum(dp[0][k])k是他的直系下属;
2,如果不选,那么dp[0][j] = 1 + sum(max(dp[0][k],dp[1][k])k是直系下属;
下面讨论是否唯一。
1,如果不选第i个人,dp[0][k]中只要有一个不唯一,那么他就不唯一;
2,如果选第i个人,dp[1][k]和dp[0][k]中比较大的决定状态,如果dp[0][k] = dp[1][k],那么他肯定不唯一。

#include<cstdio>
#include<cstring>
#include<iostream>
#include<queue>
#include<vector>
#include<algorithm>
#include<string>
#include<cmath>
#include<set>
#include<map>
#include<vector>
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int maxn = 1005;
vector<int>vt[205];
int dp[2][205],leap[2][205];
int f(int x,int y)
{
    if(dp[x][y] != -1)return dp[x][y];
    if(!vt[y].size())
    {
        dp[0][y] = 0;
        dp[1][y] = 1;
        leap[0][y] = leap[1][y] = 0;
        return dp[x][y];
    }
    dp[0][y] = 0;
    for(int i = 0;i < vt[y].size();i++)//00000000
    {
        int e = vt[y][i];
        f(0,e);f(1,e);
        if(dp[0][e] > dp[1][e])
        {
            dp[0][y] += dp[0][e];
            if(leap[0][e])leap[0][y] = 1;
        }
        else if(dp[0][e] < dp[1][e])
        {
            dp[0][y] += dp[1][e];
            if(leap[1][e])leap[0][y] = 1;
        }
        else
        {
            dp[0][y] += dp[0][e];
            leap[0][y] = 1;
        }
    }
    dp[1][y] = 1;
    for(int i = 0;i < vt[y].size();i++)
    {
        int e = vt[y][i];
        dp[1][y] += dp[0][e];
        if(leap[0][e])leap[1][y] = 1;
    }
    return dp[x][y];

}
int main()
{
    #ifdef LOCAL
    freopen("C:\\Users\\ΡΡ\\Desktop\\in.txt","r",stdin);
    //freopen("C:\\Users\\ΡΡ\\Desktop\\out.txt","w",stdout);
    #endif // LOCAL
    int n;
    while(scanf("%d",&n) != EOF)
    {
        if(!n)break;
        for(int i = 1;i <= n;i++)vt[i].clear();
        map<string,int>mp;
        string a,b;
        int cnt = 1;
        cin >> a;
        mp[a] = cnt++;
        for(int i = 1;i < n;i++)
        {
            cin >> a >> b;
            if(!mp[a])mp[a] = cnt++;
            if(!mp[b])mp[b] = cnt++;
            int u = mp[b];
            vt[u].push_back(mp[a]);
        }
        memset(leap,0,sizeof(leap));
        for(int i = 1;i <= n;i++)
            dp[0][i] = dp[1][i] = -1;
        f(0,1);
        f(1,1);
        if(dp[0][1] > dp[1][1])
        {
            printf("%d",dp[0][1]);
            if(leap[0][1])
                printf(" No\n");
            else
                printf(" Yes\n");
        }
        else if(dp[0][1] == dp[1][1])
        {
            printf("%d No\n",dp[1][1]);
        }
        else
        {
            printf("%d",dp[1][1]);
            if(leap[1][1])
                printf(" No\n");
            else
                printf(" Yes\n");
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值