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

相关文章

Spring IOC的三种实现方式详解

《SpringIOC的三种实现方式详解》:本文主要介绍SpringIOC的三种实现方式,在Spring框架中,IOC通过依赖注入来实现,而依赖注入主要有三种实现方式,构造器注入、Setter注入... 目录1. 构造器注入(Cons编程tructor Injection)2. Setter注入(Setter

Java中List转Map的几种具体实现方式和特点

《Java中List转Map的几种具体实现方式和特点》:本文主要介绍几种常用的List转Map的方式,包括使用for循环遍历、Java8StreamAPI、ApacheCommonsCollect... 目录前言1、使用for循环遍历:2、Java8 Stream API:3、Apache Commons

Python判断for循环最后一次的6种方法

《Python判断for循环最后一次的6种方法》在Python中,通常我们不会直接判断for循环是否正在执行最后一次迭代,因为Python的for循环是基于可迭代对象的,它不知道也不关心迭代的内部状态... 目录1.使用enuhttp://www.chinasem.cnmerate()和len()来判断for

虚拟机与物理机的文件共享方式

《虚拟机与物理机的文件共享方式》文章介绍了如何在KaliLinux虚拟机中实现物理机文件夹的直接挂载,以便在虚拟机中方便地读取和使用物理机上的文件,通过设置和配置,可以实现临时挂载和永久挂载,并提供... 目录虚拟机与物理机的文件共享1 虚拟机设置2 验证Kali下分享文件夹功能是否启用3 创建挂载目录4

linux报错INFO:task xxxxxx:634 blocked for more than 120 seconds.三种解决方式

《linux报错INFO:taskxxxxxx:634blockedformorethan120seconds.三种解决方式》文章描述了一个Linux最小系统运行时出现的“hung_ta... 目录1.问题描述2.解决办法2.1 缩小文件系统缓存大小2.2 修改系统IO调度策略2.3 取消120秒时间限制3

Linux alias的三种使用场景方式

《Linuxalias的三种使用场景方式》文章介绍了Linux中`alias`命令的三种使用场景:临时别名、用户级别别名和系统级别别名,临时别名仅在当前终端有效,用户级别别名在当前用户下所有终端有效... 目录linux alias三种使用场景一次性适用于当前用户全局生效,所有用户都可调用删除总结Linux

Mybatis官方生成器的使用方式

《Mybatis官方生成器的使用方式》本文详细介绍了MyBatisGenerator(MBG)的使用方法,通过实际代码示例展示了如何配置Maven插件来自动化生成MyBatis项目所需的实体类、Map... 目录1. MyBATis Generator 简介2. MyBatis Generator 的功能3

Python数据处理之导入导出Excel数据方式

《Python数据处理之导入导出Excel数据方式》Python是Excel数据处理的绝佳工具,通过Pandas和Openpyxl等库可以实现数据的导入、导出和自动化处理,从基础的数据读取和清洗到复杂... 目录python导入导出Excel数据开启数据之旅:为什么Python是Excel数据处理的最佳拍档

SpringBoot项目启动后自动加载系统配置的多种实现方式

《SpringBoot项目启动后自动加载系统配置的多种实现方式》:本文主要介绍SpringBoot项目启动后自动加载系统配置的多种实现方式,并通过代码示例讲解的非常详细,对大家的学习或工作有一定的... 目录1. 使用 CommandLineRunner实现方式:2. 使用 ApplicationRunne

VUE动态绑定class类的三种常用方式及适用场景详解

《VUE动态绑定class类的三种常用方式及适用场景详解》文章介绍了在实际开发中动态绑定class的三种常见情况及其解决方案,包括根据不同的返回值渲染不同的class样式、给模块添加基础样式以及根据设... 目录前言1.动态选择class样式(对象添加:情景一)2.动态添加一个class样式(字符串添加:情