poj 3342 Party at Hali-Bula(树形DP+判断方式是不是唯一)

1、http://poj.org/problem?id=3342

2、题目大意:

现在要邀请n个人中的一些人参加晚宴,要求是有直接上下级关系的人不能同时出席,问最多可以邀请多少人参加,并判断在保证最大人数的情况下,人是不是唯一确定的

状态转移方程很好确定,难在怎么判断方案是不是唯一,假设第i个人参加时值最大,那么不能确定是不是不唯一,但是如果第i个人不去的方案最优的话,他的状态有两种,孩子要么去,要么不去,如果这两种状态的最大值相同,那么就是不唯一的,详见代码

3、题目:

Party at Hali-Bula
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 4917 Accepted: 1741

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

6
Jason
Jack Jason
Joe Jack
Jill Jason
John Jack
Jim Jill
2
Ming
Cho Ming
0

Sample Output

4 Yes
1 No

 

4、AC代码:

#include<stdio.h>
#include<string.h>
#include<string>
#include<algorithm>
#include<map>
#include<vector>
using namespace std;
vector<int> vec[205];
map<string,int> mp;
char str[205][105];
char tmp[105];
char a[105],b[105];
int dp[205][2];
int k;
int find(char a[105])
{
    for(int i=0; i<k; i++)
    {
        if(strcmp(a,str[i])==0)
            return i;
    }
    strcpy(str[k++],a);
    return k-1;
}
void dfs(int root)
{
    for(int i=0; i<vec[root].size(); i++)
    {
        int v=vec[root][i];
        dfs(v);
        dp[root][0]+=max(dp[v][0],dp[v][1]);
        dp[root][1]+=dp[v][0];
    }
}
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        if(n==0)
            break;
        k=0;
        mp.clear();
        scanf("%s",tmp);
        mp[tmp]=k++;
        for(int i=0; i<n; i++)
        {
            vec[i].clear();
            dp[i][0]=0;
            dp[i][1]=1;
        }
        for(int i=1; i<n; i++)
        {
            scanf("%s%s",a,b);
            if(mp.find(a)==mp.end()) mp[a]=k++;
            if(mp.find(b)==mp.end()) mp[b]=k++;
            vec[mp[b]].push_back(mp[a]);
        }
        dfs(0);
        //下面代码判断是不是唯一的方案
        int flag=1;
        for(int i=0; i<n; i++)
        {
            if(dp[i][0]>dp[i][1])
            {
                for(int j=0; j<vec[i].size(); j++)
                {
                    int v=vec[i][j];
                    if(dp[v][0]==dp[v][1])
                    {
                        flag=0;
                        break;
                    }
                }
            }
            if(flag==0)
            break;
        }
        if(flag==0 || dp[0][0]==dp[0][1])//注意判断条件,
            printf("%d No\n",max(dp[0][0],dp[0][1]));
        else
            printf("%d Yes\n",max(dp[0][0],dp[0][1]));
    }
    return 0;
}
/*
6
Jason
Jack Jason
Joe Jack
Jill Jason
John Jack
Jim Jill
2
Ming
Cho Ming
0
*/

5、AC代码,处理字符串用的for循环遍历的,没有用map


 

#include<stdio.h>
#include<string.h>
#include<vector>
using namespace std;
vector<int> vec[205];
char str[205][105];
char tmp[105];
char tmp1[105];
int dp[205][2];
int k;
int find(char a[105])
{
    for(int i=0; i<k; i++)
    {
        if(strcmp(a,str[i])==0)
            return i;
    }
    strcpy(str[k++],a);
    return k-1;
}
void dfs(int root)
{
    for(int i=0; i<vec[root].size(); i++)
    {
        int v=vec[root][i];
        dfs(v);
        dp[root][0]+=max(dp[v][0],dp[v][1]);
        dp[root][1]+=dp[v][0];
    }
}
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        if(n==0)
            break;
        k=0;
        scanf("%s",tmp);
        strcpy(str[k++],tmp);
        for(int i=0; i<n; i++)
        {
            vec[i].clear();
            dp[i][0]=0;
            dp[i][1]=1;
        }
        for(int i=1; i<n; i++)
        {
            scanf("%s%s",tmp,tmp1);
            int a=find(tmp);
            int b=find(tmp1);
            vec[b].push_back(a);
        }
        dfs(0);
        int flag=0;
        for(int i=0; i<n; i++)
        {
            if(dp[i][0]>=dp[i][1])
            {
                for(int j=0; j<vec[i].size(); j++)
                {
                    int v=vec[i][j];
                    if(dp[v][0]==dp[v][1])
                    {
                        flag=1;
                        break;
                    }
                }
            }
            if(flag==1)
            break;
        }
        if(flag==1 || dp[0][0]==dp[0][1])
            printf("%d No\n",max(dp[0][0],dp[0][1]));
        else
            printf("%d Yes\n",max(dp[0][0],dp[0][1]));
    }
    return 0;
}
/*
6
Jason
Jack Jason
Joe Jack
Jill Jason
John Jack
Jim Jill
2
Ming
Cho Ming
0
*/


 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
POJ - 3616是一个题目,题目描述如下: 给定一组区间,每个区间有一个权重,要求选择一些区间,使得这些区间的右端点都小于等于k,并且权重之和最大。请问最大的权重和是多少? 解决这个问题的思路是使用动态规划。首先,将区间按照左端点从小到大进行排序。然后,定义一个dp数组,dp[i]表示右端点小于等于i的所有区间所能得到的最大权重。 接下来,遍历每一个区间,对于每个区间i,将dp[i]初始化为区间i的权重。然后,再遍历i之前的每个区间j,如果区间j的右端点小于等于k,并且区间j的权重加上区间i的权重大于dp[i],则更新dp[i]为dp[j]加上区间i的权重。 最后,遍历整个dp数组,找到最大的权重和,即为所求的答案。 下面是具体的代码实现: ```cpp #include <cstdio> #include <cstring> #include <algorithm> using namespace std; struct interval{ int start, end, weight; }; interval intervals[10005]; int dp[10005]; int n, m, k; bool compare(interval a, interval b) { if (a.start == b.start) { return a.end < b.end; } else { return a.start < b.start; } } int main() { while(~scanf("%d %d %d", &n, &m, &k)) { memset(dp, 0, sizeof dp); for (int i = 0; i < m; i++) { scanf("%d %d %d", &intervals[i].start, &intervals[i].end, &intervals[i].weight); } sort(intervals, intervals + m, compare); for (int i = 0; i < m; i++) { dp[i] = intervals[i].weight; for (int j = 0; j < i; j++) { if (intervals[j].end <= k && dp[j] + intervals[i].weight > dp[i]) { dp[i] = dp[j] + intervals[i].weight; } } } int maxWeight = 0; for (int i = 0; i < m; i++) { maxWeight = max(maxWeight, dp[i]); } printf("%d\n", maxWeight); } } ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值