Marriage is Stable
Marriage is Stable |
| Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) |
| Total Submission(s): 64 Accepted Submission(s): 43 |
|
Problem Description
Albert, Brad, Chuck are happy bachelors who are in love with Laura, Marcy, Nancy. They all have three choices. But in fact, they do have some preference in mind. Say Albert, he likes Laura best, but that doesn't necesarily mean Laura likes him. Laura likes Chuck more than Albert. So if Albert can't marry Laura, he thinks Nancy a sensible choice. For Albert, he orders the girls Laura > Nancy > Marcy.
For the boys: Albert: Laura > Nancy > Marcy For the girls: Laura: Chuck > Albert > Brad But if they were matched randomly, such as Albert <-> Laura they would soon discover it's not a nice solution. For Laura, she likes Chuck instead of Albert. And what's more, Chuck likes Laura better than Nancy. So Laura and Chuck are likely to come together, leaving poor Albert and Nancy. Now it's your turn to find a stable marriage. A stable marriage means for any boy G and girl M, with their choice m[G] and m[M], it will not happen that rank(G, M) < rank(G, m[G])and rank(M, G) < rank(M, m[M]). |
|
Input
Each case starts with an integer n (1 <= n <= 500), the number of matches to make.
The following n lines contain n + 1 names each, the first being name of the boy, and rest being the rank of the girls. The following n lines are the same information for the girls. Process to the end of file. |
|
Output
If there is a stable marriage, print n lines with two names on each line. You can choose any one if there are multiple solution. Print "Impossible" otherwise.
Print a blank line after each test. |
|
Sample Input
3 |
|
Sample Output
Albert Nancy |
|
Author
CHENG, Long
|
|
Source
ZOJ
|
|
Recommend
8600
|
/*
题意:给你n个男生暗恋的对象,n个女生暗恋的对象,如果刚好能组成n对不重复的情侣,就输出,如果不可能的话,就输出Impossible 初步思路:很典型的二分匹配问题
*/
#include<bits/stdc++.h>
using namespace std;
/***********************二分匹配模板**************************/
const int MAXN=;
int g[MAXN][MAXN];//编号是0~n-1的
int linker[MAXN];//记录匹配点i的匹配点是谁
bool used[MAXN];
map<string,int> m;
map<int ,string> M;
int len=;
int n;
string bname,gname;
bool dfs(int u)//回溯看能不能通过分手来进行匹配
{
int v;
for(v=;v<n*;v++)
if(g[u][v]&&!used[v])
//如果有这条边,并且这条边没有用过
{
used[v]=true;
if(linker[v]==-||dfs(linker[v]))//如果这个点没有匹配过,并且能找到匹配点,那么就可以以这个边作为匹配点
{
linker[v]=u;
return true;
}
}
return false;
}
int hungary()//返回最大匹配数
{
int res=;
int u;
memset(linker,-,sizeof(linker));
for(u=;u<n*;u++)
{
memset(used,,sizeof(used));
if(dfs(u))//如果这个点有匹配点
res++;
}
return res;
}
/***********************二分匹配模板**************************/
void init(){
len=;
memset(g,,sizeof g);
}
int main(){
// freopen("in.txt","r",stdin);
while(scanf("%d",&n)!=EOF){
init();
for(int i=;i<n;i++){
cin>>bname;
m[bname]=i;
M[i]=bname;
for(int j=;j<n;j++){
cin>>gname;
if(m.find(gname)==m.end()){
m[gname]=++len;
M[len]=gname;
}
g[m[bname]][m[gname]]=;
}
}
// for(int i=0;i<n*2;i++){
// cout<<M[i]<<" ";
// }cout<<endl; for(int i=;i<n;i++){
cin>>gname;
for(int j=;j<n;j++){
cin>>bname;
g[m[gname]][m[bname]]=;
}
}
// for(int i=0;i<n*2;i++){
// for(int j=0;j<n*2;j++){
// cout<<g[i][j]<<" ";
// }cout<<endl;
// }
//cout<<hungary()<<endl;
if(hungary()==n*){
for(int i=;i<n;i++){
cout<<M[i]<<" "<<M[linker[i]]<<endl;
}
}else{
puts("Impossible");
}
}
return ;
}
Marriage is Stable的更多相关文章
- HDU 1522 Marriage is Stable 稳定婚姻匹配
http://acm.hdu.edu.cn/showproblem.php?pid=1522 #include<bits/stdc++.h> #define INF 0x3f3f3f3f ...
- HDU 1522 Marriage is Stable 【稳定婚姻匹配】(模板题)
<题目链接> 题目大意: 给你N个男生和N个女生,并且给出所有男生和女生对其它所有异性的喜欢程度,喜欢程度越高的两个异性越容易配对,现在求出它们之间的稳定匹配. 解题分析: 稳定婚姻问题的 ...
- Marriage is Stable HDU1522 稳定婚姻问题基础
几对男女 给出每个人心中的优先级 进行最合理的匹配 要打印名字的话必须有一个名字数组 英文名用map 稳定婚姻问题: 每次循环遍历所有的男的 每个男的对目前未被拒绝的并且优先级最高的进行预匹配 ...
- 【转】稳定婚姻问题(Stable Marriage Problem)
转自http://www.cnblogs.com/drizzlecrj/archive/2008/09/12/1290176.html 稳定婚姻是组合数学里面的一个问题. 问题大概是这样:有一个社团里 ...
- acm数学(转)
这个东西先放在这吧.做过的以后会用#号标示出来 1.burnside定理,polya计数法 这个大家可以看brudildi的<组合数学>,那本书的这一章写的很详细也很容易理解.最好能 ...
- [转] POJ数学问题
转自:http://blog.sina.com.cn/s/blog_6635898a0100magq.html 1.burnside定理,polya计数法 这个大家可以看brudildi的<组合 ...
- 【转载】图论 500题——主要为hdu/poj/zoj
转自——http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并 ...
- POJ题目细究
acm之pku题目分类 对ACM有兴趣的同学们可以看看 DP: 1011 NTA 简单题 1013 Great Equipment 简单题 102 ...
- 【HDOJ图论题集】【转】
=============================以下是最小生成树+并查集====================================== [HDU] How Many Table ...
随机推荐
- StringBuffer类的构造方法
public StringBuffer():无参构造方法 public StringBuffer(int capacity):指定容量的字符串缓冲区对象(默认是16个字符) public String ...
- STM32获取DHT11温度传感器数据
准备物件 STM32F103C8T6核心板 ST-LINK V2 DHT11 杜邦线若干 连接线 STM32F103C8T6芯片管脚图 管脚说明 连接仿真器 STM32 ST-LINKV2 VCC V ...
- 深入理解计算机系统chapter7
链接:将各种代码和数据部分收集起来并组合成为单一文件的过程,这个文件可被加载到存储器并执行. 在运行时,和一个在存储器中的程序链接起来 二.静态链接库与动态链接库 静态连接库就是把(lib)文件中用到 ...
- hive自定义UDF
udf udaf udtf 使用方式 hiverc文件 1.jar包放到安装日录下或者指定目录下 2.${HIVE_HOME}/bin目录下有个.hiverc文件,它是隐藏文件. 3.把初始化语句加载 ...
- Mybatis Dynamic Query 2.0.2
项目地址:https://github.com/wz2cool/mybatis-dynamic-query 文档地址:https://wz2cool.gitbooks.io/mybatis-dynam ...
- Flip Game poj 1753
Flip Game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 29731 Accepted: 12886 Descr ...
- swiper拖拽之后不自动滑动问题
//swiper轮播图 var mySwiper = new Swiper('.swiper-container',{ initialSlide :0, autoplay : 3000, direct ...
- ubuntu访问文件服务器
随便打开一个文件夹,按ctrl+L 输入 smb://172.29.17.1 然后输入用户名和密码即可
- KM算法的应用
HDU2255 模板 难度x HDU2282 思维 难度XXx HDU3722 模板 难度X HDU3395 模版 HDU1533 最小值模型 难度x HDU2853 HDU3 ...
- JAVA提高三:反射总结
为前期学习过反射,再这里再次复习总结下:[转载请说明来源:http://www.cnblogs.com/pony1223/p/7659210.html ] 一.透彻分析反射的基础_Class类 Cla ...