UVA 1220 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

#include<iostream>
#include<algorithm>
#include<string>
#include<map>//int dx[4]={0,0,-1,1};int dy[4]={-1,1,0,0};
#include<set>//int gcd(int a,int b){return b?gcd(b,a%b):a;}
#include<vector>
#include<cmath>
#include<stack>
#include<string.h>
#include<stdlib.h>
#include<cstdio>
#define mod 1e9+7
#define ll long long
#define maxn 205
#define MAX 1000000000
#define ms memset
using namespace std;

int n,cnt;
int  flag[maxn][2];///从属关系
int dp[maxn][2];///0代表不选根,1代表选根
/*
题目大意:n个人形成树状结构,除老板外每个员工都有唯一的编号,
选尽量多的人但不能同时选择他和其上属,且选法是否唯一也要输出。

树状的最大独立集的问题。
d(u,1),d(u,0)分别表示选根和没选根的子树的最大独立集。
那么递推过程可分析出来,d(u,1)选了根则子节点均不能选,
所以答案为其子节点sigma d(sons,0),其中如果有一个子节点答案不唯一则根节点不唯一。

如果没选根,子节点可选可不选,sigma (max(d(sons,0),d(sons,1)));
其中的flag如果子节点两种状态都一样则flag为0,如果不一样则判断选定的那个子节点的状态即可

过程中我用的记忆化搜索,因为状态可能有重叠,因为是树状结构,dfs复杂度不会太高
*/
vector<int> sons[maxn];
void read()
{
    memset(flag,0,sizeof(flag));
    memset(dp,0xff,sizeof(dp));

    for(int i=0;i<maxn;i++)   sons[i].clear();

    char tp[maxn][maxn];  cnt=0;

    char name1[maxn] , name2[maxn];
    scanf("%s",name1);

    strcpy(tp[cnt++],name1);

    for(int i=0;i<n-1;i++)
    {
        scanf("%s%s",name1,name2);

        int u=0;
        for(;u<cnt;u++)
            if(strcmp(tp[u],name1)==0)  break;
        if(u==cnt) strcpy(tp[cnt++],name1);

        int v=0;
        for(;v<cnt;v++)
            if(strcmp(tp[v],name2)==0) break;
        if(v==cnt) strcpy(tp[cnt++],name2);

        sons[v].push_back(u);
    }

    for(int i=0;i<cnt;i++)
        if(sons[i].empty())
        {
            dp[i][0]=0;
             dp[i][1]=1;
             flag[i][0]=1,flag[i][1]=1;///一个点时独立集是唯一的
        }
}

int Dp(int u,int v)///记忆化搜索
{
    if(dp[u][v]>-1) return dp[u][v];
    if(sons[u].empty())   return v;

    int len=sons[u].size();
    int res1=0 , res2=0;

    flag[u][v]=1;

    if(v)
    {
        res1++;
        for(int i=0;i<len;i++)
        {
            res1+= Dp(sons[u][i],0);
            flag[u][v] &= flag[sons[u][i]][0];
        }
        return dp[u][v]=res1;
    }
    else
    {
        dp[u][v]=0;
        for(int i=0;i<len;i++)
        {
            int res1= Dp(sons[u][i],0);
            int res2= Dp(sons[u][i],1);
            
            dp[u][v]+=max(res1,res2);
            
            if(res1==res2) flag[u][v]=0;
            else if(res1>res2&& flag[sons[u][i]][0]==0) flag[u][v]=0;
            else if(res1<res2 && flag[sons[u][i]][1]==0) flag[u][v]=0;
        }
        return dp[u][v];
    }
}

int main()
{
    while(scanf("%d",&n) && n)
    {
        read();
        int ans1=Dp(0,0);
        int ans2=Dp(0,1);

        if(ans1==ans2)
        {
            printf("%d ",ans1);
            puts("No");
        }
        else if(ans1>ans2)
        {
            printf("%d ",ans1);
            if(flag[0][0]) puts("Yes");
            else puts("No");
        }
        else if(ans2>ans1)
        {
            printf("%d ",ans2);
            if(flag[0][1]) puts("Yes");
            else puts("No");
        }
    }
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值