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

2023-11-08 12:08

本文主要是介绍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]));elseprintf("%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]));elseprintf("%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
*/


 

 

这篇关于poj 3342 Party at Hali-Bula(树形DP+判断方式是不是唯一)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/369752

相关文章

Android实现定时任务的几种方式汇总(附源码)

《Android实现定时任务的几种方式汇总(附源码)》在Android应用中,定时任务(ScheduledTask)的需求几乎无处不在:从定时刷新数据、定时备份、定时推送通知,到夜间静默下载、循环执行... 目录一、项目介绍1. 背景与意义二、相关基础知识与系统约束三、方案一:Handler.postDel

gradle第三方Jar包依赖统一管理方式

《gradle第三方Jar包依赖统一管理方式》:本文主要介绍gradle第三方Jar包依赖统一管理方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录背景实现1.顶层模块build.gradle添加依赖管理插件2.顶层模块build.gradle添加所有管理依赖包

Linux之systemV共享内存方式

《Linux之systemV共享内存方式》:本文主要介绍Linux之systemV共享内存方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、工作原理二、系统调用接口1、申请共享内存(一)key的获取(二)共享内存的申请2、将共享内存段连接到进程地址空间3、将

Maven中引入 springboot 相关依赖的方式(最新推荐)

《Maven中引入springboot相关依赖的方式(最新推荐)》:本文主要介绍Maven中引入springboot相关依赖的方式(最新推荐),本文给大家介绍的非常详细,对大家的学习或工作具有... 目录Maven中引入 springboot 相关依赖的方式1. 不使用版本管理(不推荐)2、使用版本管理(推

判断PyTorch是GPU版还是CPU版的方法小结

《判断PyTorch是GPU版还是CPU版的方法小结》PyTorch作为当前最流行的深度学习框架之一,支持在CPU和GPU(NVIDIACUDA)上运行,所以对于深度学习开发者来说,正确识别PyTor... 目录前言为什么需要区分GPU和CPU版本?性能差异硬件要求如何检查PyTorch版本?方法1:使用命

C#使用StackExchange.Redis实现分布式锁的两种方式介绍

《C#使用StackExchange.Redis实现分布式锁的两种方式介绍》分布式锁在集群的架构中发挥着重要的作用,:本文主要介绍C#使用StackExchange.Redis实现分布式锁的... 目录自定义分布式锁获取锁释放锁自动续期StackExchange.Redis分布式锁获取锁释放锁自动续期分布式

Java对象转换的实现方式汇总

《Java对象转换的实现方式汇总》:本文主要介绍Java对象转换的多种实现方式,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录Java对象转换的多种实现方式1. 手动映射(Manual Mapping)2. Builder模式3. 工具类辅助映

Spring Boot读取配置文件的五种方式小结

《SpringBoot读取配置文件的五种方式小结》SpringBoot提供了灵活多样的方式来读取配置文件,这篇文章为大家介绍了5种常见的读取方式,文中的示例代码简洁易懂,大家可以根据自己的需要进... 目录1. 配置文件位置与加载顺序2. 读取配置文件的方式汇总方式一:使用 @Value 注解读取配置方式二

JAVA保证HashMap线程安全的几种方式

《JAVA保证HashMap线程安全的几种方式》HashMap是线程不安全的,这意味着如果多个线程并发地访问和修改同一个HashMap实例,可能会导致数据不一致和其他线程安全问题,本文主要介绍了JAV... 目录1. 使用 Collections.synchronizedMap2. 使用 Concurren

Python如何精准判断某个进程是否在运行

《Python如何精准判断某个进程是否在运行》这篇文章主要为大家详细介绍了Python如何精准判断某个进程是否在运行,本文为大家整理了3种方法并进行了对比,有需要的小伙伴可以跟随小编一起学习一下... 目录一、为什么需要判断进程是否存在二、方法1:用psutil库(推荐)三、方法2:用os.system调用