[ACM_图论] Sorting Slides(挑选幻灯片,二分匹配,中等)
Description
The situation is like this. The slides all have numbers written on them according to their order in the talk. Since the slides lie on each other and are transparent, one cannot see on which slide each number is written.
Well, one cannot see on which slide a number is written, but one may deduce which numbers are written on which slides. If we label the slides which characters A, B, C, ... as in the figure above, it is obvious that D has number 3, B has number 1, C number 2 and A number 4.
Your task, should you choose to accept it, is to write a program that automates this process.
Input
The input consists of several heap descriptions. Each heap descriptions starts with a line containing a single integer n, the number of slides in the heap. The following n lines contain four integers xmin, xmax, ymin and ymax, each, the bounding coordinates of the slides. The slides will be labeled as A, B, C, ... in the order of the input.
This is followed by n lines containing two integers each, the x- and y-coordinates of the n numbers printed on the slides. The first coordinate pair will be for number 1, the next pair for 2, etc. No number will lie on a slide boundary.
The input is terminated by a heap description starting with n = 0, which should not be processed.
Output
For each heap description in the input first output its number. Then print a series of all the slides whose numbers can be uniquely determined from the input. Order the pairs by their letter identifier.
If no matchings can be determined from the input, just print the word none on a line by itself.
Output a blank line after each test case.
Sample Input
4
6 22 10 20
4 18 6 16
8 20 2 18
10 24 4 8
9 15
19 17
11 7
21 11
2
0 2 0 2
0 2 0 2
1 1
1 1
0
Sample Output
Heap 1
(A,4) (B,1) (C,2) (D,3)
Heap 2
none
题目大意:给你n个幻灯片(每个幻灯片用坐标xmin,xmax,ymin,ymax表示),然后给出n个标号的坐标(x,y)表示,让你依次输出能确定的幻灯片所对应的标号,如果没有一个可以确定,就输出none。
解题思路:这题是二分图匹配问题,我们不妨把标号看成X集合,把幻灯片看成Y集合,然后依次删除某一个对应关系并看是否为完美匹配,如果不是,说明删除的边为必须的(也就是唯一确定关系),就这样枚举完所有关系就可得出所有确定关系!!!
相关链接:如果想学二分匹配的话请参阅:The Perfect Stall 完美的牛栏(匈牙利算法、最大二分匹配)
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <memory.h>
using namespace std;
int n;
//------------------------------------------------------------------------
int tab[][];//邻接矩阵,第一维对标号,第二维对应幻灯片
int state[],result[];//stata:是否被搜索过;result:某幻灯片对应的标号
int ans;//找到多少匹配
//------------------------------------------------------------------------
int find(int x){// 匈牙利算法---增广路部分(给x找匹配,找到就返回1,否则返回0)
for(int i=;i<=n;i++){
if(tab[x][i]== && !state[i]){
state[i]=;//标记为搜索过
if(result[i]== || find(result[i])){//未被匹配过&&能找到一条增广路
result[i]=x;//匹配i,x
return ;//能找到新的匹配
}
}
}
return ;
}
int XYL(){//匈牙利算法----主程部分(返回最大匹配)
ans=;
memset(result,,sizeof(result));
for(int i=;i<=n;i++){//从小到大匹配X,如果匹配成功就ans++
memset(state,,sizeof(state));//清空是否搜索过数组
if(find(i)) ans++;//找到新的匹配
}//完成后result[i]保存着第i个幻灯片对应的标号
return ans;
}
//------------------------------------------------------------------------
int main(){
int casee=;
while(cin>>n){
memset(tab,,sizeof(tab));
//输入及预处理(输入数据维护0-1矩阵tab[标号][幻灯片]::从1开始::)
int rect[][];//n个矩形
if(n==)break;
for(int i=;i<=n;i++)//输入n个矩形
for(int j=;j<;j++)
cin>>rect[i][j];
for(int i=;i<=n;i++){//输入n个点并填充tab[][]邻接数组
int x,y;
cin>>x>>y;
for(int j=;j<=n;j++)
if(rect[j][]<x && rect[j][]>x && rect[j][]<y && rect[j][]>y)
tab[i][j]=;
}
//输出(核心部分)
printf("Heap %d\n",casee++);
bool ok=;//标记是否有可以确定的关系,如果没有输出none
for(int j=;j<=n;j++){//枚举所有关系
for(int i=;i<=n;i++){
if(!tab[i][j])continue;//没有关系直接跳过
tab[i][j]=;//将该关系断开求匹配数(如果不是完美匹配,说明该关系唯一!!!)输出
if(XYL()<n){
if(ok)printf(" ");//这是输出格式,要注意空格!!!
ok=;
char s=j+'A'-;
printf("(%c,%d)",s,i);
}
tab[i][j]=;//最后再把该关系恢复,看其它关系
}
}
if(!ok) printf("none\n\n");
else printf("\n\n");
}return ;
}
//ps:如果想看二分匹配或者搞匈牙利算法模板的话可以参阅我的另一篇博文
//----->_<----The Perfect Stall 完美的牛栏(匈牙利算法、最大二分匹配)
[ACM_图论] Sorting Slides(挑选幻灯片,二分匹配,中等)的更多相关文章
- POJ 1486 Sorting Slides(二分图完全匹配必须边)题解
题意:给你n张照片的范围,n个点的坐标,问你能唯一确定那几个点属于那几张照片,例如样例中4唯一属于A,2唯一属于C,1唯一属于B,3唯一属于C 思路:进行二分图完全匹配,怎么判断唯一属于?匹配完之后删 ...
- [ACM_图论] The Perfect Stall 完美的牛栏(匈牙利算法、最大二分匹配)
描述 农夫约翰上个星期刚刚建好了他的新牛棚,他使用了最新的挤奶技术.不幸的是,由于工程问题,每个牛栏都不一样.第一个星期,农夫约翰随便地让奶牛们进入牛栏,但是问题很快地显露出来:每头奶牛都只愿意在她们 ...
- UVA663 Sorting Slides(烦人的幻灯片)
UVA663 Sorting Slides(烦人的幻灯片) 第一次做到这么玄学的题,在<信息学奥赛一本通>拓扑排序一章找到这个习题(却发现标程都是错的),结果用二分图匹配做了出来 蒟蒻感觉 ...
- POJ 1486 Sorting Slides【二分图匹配】
题目大意:有n张幻灯片和n个数字,幻灯片放置有重叠,每个数字隶属于一个幻灯片,现在问你能够确定多少数字一定属于某个幻灯片 思路:上次刷过二分图的必须点后这题思路就显然了 做一次二分匹配后将当前匹配的边 ...
- poj 1486 Sorting Slides
Sorting Slides Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 4469 Accepted: 1766 De ...
- 【HDU 2255】奔小康赚大钱 (最佳二分匹配KM算法)
奔小康赚大钱 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Subm ...
- [置顶] 白话二分匹配之最大匹配+附上hdu2063解题报告
最近开始学习图论的二分匹配,关于最大匹配做一次小总结,希望自己后面回头来看一目明了,也对刚接触的人有帮助: ps:开始有的文字很多....对于很多人来说一看到文字就烦啦...不过这个总结是针对匈牙利算 ...
- POJ1468 Sorting Slides
Sorting Slides Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 4442 Accepted: 1757 De ...
- POJ 1486 Sorting Slides (KM)
Sorting Slides Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 2831 Accepted: 1076 De ...
随机推荐
- HDOJ(1728)逃离迷宫
HDOJ 1728 http://acm.hdu.edu.cn/showproblem.php?pid=1728 BFS求最少转过的弯 #include <stdio.h> #includ ...
- I/O阻塞非阻塞,同步异步
http://www.cnblogs.com/luotianshuai/p/5098408.html "阻塞"与"非阻塞"与"同步"与&qu ...
- [笔记]Altera系列01:常用资料下载链接
Altera官方文档 Altera Product Catalog 外部存储器规范估算器 To be continued.
- iOS崩溃日志记录工具--CrashlyTics
http://try.crashlytics.com Crashlytics优势: 1.Crashlytics基本不会漏掉任何应用崩溃的信息 2.Crashlytics对崩溃日志管理很人性化,会根据崩 ...
- GsonWithoutObject 没有对象(脱离对象) 直接提取 ... gson json
GsonWithoutObject 脱离对象, 直接提取 package temp; import tool.FileTool; import com.google.gson.JsonElement; ...
- PHP 判断是否为Get/Post/Ajax提交
<?php PHP 判断是否为Get/Post/Ajax提交 /** * 是否是AJAx提交的 * @return bool */ function isAjax(){ if(isset($_S ...
- 开源PLM软件Aras详解四 ItemType的概念
首先,我们需要了解什么是ItemType,俗称对象类 官方一点就是ItemType是一个用来定义业务对象类的业务对象类 通过ItemType定义的每个业务对象类可以产生各自的实例对象 通过ItemTy ...
- myBatis自动生成mapping,dao和model
myBatis没有自动封装实体类.所以都需要自己编写,但是表数据过多时.编写难度就会增加,错误率也会增加! 所以myBatis提供mybatis-generator-core-1.3.2-bundle ...
- Windows XP SP3下编译安装openssl-1.1.0b
软件需要: openssl-1.1.0b visual studio 2010(cn_visual_studio_2010_ultimate_x86_dvd_532347.iso) perl:Acti ...
- MYSQL存储过程:批量更新数据
地区等级的信息储存在jsjh_district表. 要更新jsjh_goods_district表的district_level地区信息 DELIMITER $$ DROP PROCEDURE IF ...