POJ 3342 Party at Hali-Bula (树形dp 树的最大独立集 判多解 好题)

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

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

Source

Tehran 2006

题目链接:http://poj.org/problem?id=3342

题目大意:一棵树,父亲和儿子不能同一时候选入同一个集合,如今求能选集合中元素个数最多的那个集合大小。并推断解是否唯一

题目分析:求树的最大独立集的问题。好题,也用了两次dp的思想,有点类似HDU 5282这题的思想
dp[i][0]表示不选第i个结点,集合大小的最大值
dp[i][1]表示选第i个结点,集合大小的最大值
对于此dp显然
dp[i][1] = dp[son][0]  选父亲则不能选儿子
dp[i][0] = max(dp[son][0], dp[son][1]) 不选父亲的话则值等于选儿子或者不选儿子里的较大值

s[i][1] == true 表示选第i个结点时有唯一解,false表示解不唯一
s[i][0] == true 表示不选第i个结点时有唯一解。false表示解不唯一
開始时设s[i][1],s[i][0]都为true,对于此dp。我们主要考虑父亲的解变成不唯一的情况
与第一个dp状态相应,分成选父亲和不选父亲两种情况
if(!s[son][0])  s[i][1] = false,意思是假设不选儿子时有多个解,则此时能够选父亲,选父亲也肯定有多个解
if(dp[son][0] == dp[son][1])  s[i][0] = false。假设选不选儿子的答案同样。显然不选父亲时有多个解,由于选不选儿子都能够

最后自叶子向根回溯求解推断就可以。这题由于map没清零,wa了大半天。

。。



#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <vector>
#include <map>
#include <iostream>
using namespace std;
int const MAX = 300;
int n;
int dp[MAX][2];
bool s[MAX][2];
bool vis[MAX];
vector <int> vt[MAX];

void DFS(int fa)
{
    dp[fa][1] = 1;
    vis[fa] = true;
    s[fa][0] = true;
    s[fa][1] = true;
    int sz = vt[fa].size();
    for(int i = 0; i < sz; i++)
    {
        int son = vt[fa][i];
        if(!vis[son])
        {
            DFS(son);
            dp[fa][1] += dp[son][0];
            dp[fa][0] += max(dp[son][0], dp[son][1]);
            if(dp[son][0] == dp[son][1])
                s[fa][0] = false;
            if(!s[son][0])
                s[fa][1] = false;
        }
    } 
    return;
}

int main()
{
    while(scanf("%d", &n) != EOF && n)
    {
        map <string, int> mp;
        for(int i = 0; i < MAX; i++)  
            vt[i].clear();  
        memset(vis, false, sizeof(vis));
        memset(dp, 0, sizeof(dp));
        int cnt = 0;
        string boss, fir, sec;
        cin >> boss;
        mp[boss] = cnt ++;
        for(int i = 0; i < n - 1; i++)
        {
            cin >> sec >> fir;
            if(!mp.count(fir))
                mp[fir] = cnt ++;
            if(!mp.count(sec))
                mp[sec] = cnt ++;
            vt[mp[fir]].push_back(mp[sec]);
        }
        DFS(0);
        if(dp[0][1] > dp[0][0] && s[0][1])
            printf("%d Yes\n", dp[0][1]);
        else if(dp[0][1] < dp[0][0] && s[0][0])
            printf("%d Yes\n", dp[0][0]);
        else
            printf("%d No\n", max(dp[0][0] , dp[0][1]));
    }
}


转载于:https://www.cnblogs.com/gavanwanggw/p/6851549.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
基于Django+python编写开发的毕业生就业管理系统支持学生教师角色+db数据库(毕业设计新项目).zip 【备注】 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,放心下载使用!有问题及时沟通交流。 2、适用人群:计算机相关专业(如计科、信息安全、数据科学与大数据技术、人工智能、通信、物联网、自动化、电子信息等)在校学生、专业老师或者企业员工下载使用。 3、用途:项目具有较高的学习借鉴价值,不仅适用于小白学习入门进阶。也可作为毕设项目、课程设计、大作业、初期项目立项演示等。 4、如果基础还行,或热爱钻研,亦可在此项目代码基础上进行修改添加,实现其他不同功能。 欢迎下载!欢迎交流学习!不清楚的可以私信问我! 基于Django+python编写开发的毕业生就业管理系统支持学生教师角色+db数据库(毕业设计新项目).zip基于Django+python编写开发的毕业生就业管理系统支持学生教师角色+db数据库(毕业设计新项目).zip基于Django+python编写开发的毕业生就业管理系统支持学生教师角色+db数据库(毕业设计新项目).zip基于Django+python编写开发的毕业生就业管理系统支持学生教师角色+db数据库(毕业设计新项目).zip基于Django+python编写开发的毕业生就业管理系统支持学生教师角色+db数据库(毕业设计新项目).zip基于Django+python编写开发的毕业生就业管理系统支持学生教师角色+db数据库(毕业设计新项目).zip基于Django+python编写开发的毕业生就业管理系统支持学生教师角色+db数据库(毕业设计新项目).zip基于Django+python编写开发的毕业生就业管理系统支持学生教师角色+db数据库(毕业设计新项目).zip基于Django+python编写开发的毕业生就业管理系统支持学生教师角色+db数据库(毕业设计新项目).zip
毕设新项目基于python3.7+django+sqlite开发的学生就业管理系统源码+使用说明(含vue前端源码).zip 【备注】 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,放心下载使用!有问题及时沟通交流。 2、适用人群:计算机相关专业(如计科、信息安全、数据科学与大数据技术、人工智能、通信、物联网、自动化、电子信息等)在校学生、专业老师或者企业员工下载使用。 3、用途:项目具有较高的学习借鉴价值,不仅适用于小白学习入门进阶。也可作为毕设项目、课程设计、大作业、初期项目立项演示等。 4、如果基础还行,或热爱钻研,亦可在此项目代码基础上进行修改添加,实现其他不同功能。 欢迎下载!欢迎交流学习!不清楚的可以私信问我! 学生就业管理系统(前端) ## 项目开发环境 - IDE: vscode - node版本: v12.14.1 - npm版本: 6.13.4 - vue版本: @vue/cli 4.1.2 - 操作系统: UOS 20 ## 1.进入项目目录安装依赖 ``` npm install ``` ## 2.命令行执行进入UI界面进行项目管理 ``` vue ui ``` ## 3.编译发布包(注意编译后存储路径) #### PS:需要将编译后的包复制到后端项目的根目录下并命名为'static' 学生就业管理系统(后端) ## 1.项目开发环境 - IDE: vscode - Django版本: 3.0.3 - Python版本: python3.7.3 - 数据库 : sqlite3(测试专用) - 操作系统 : UOS 20 ## 2.csdn下载本项目并生成/安装依赖 ``` pip freeze > requirements.txt pip install -r requirements.txt ``` ## 3.项目MySQL数据库链接错误 [点击查看解决方法](https://www.cnblogs.com/izbw/p/11279237.html)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值