[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 ...
随机推荐
- 远程桌面Default.rdp 中各个参数的含义
存储在 Default.rdp 文件中的设置 默认情况下,将在“我的文档”文件夹中创建 Default.rdp 文件.以下 RDP 设置存储在 Desktop.rdp 文件中: desktopwidt ...
- 记一次未解决的异常:java.lang.NoClassDefFoundError: net/sf/json/JSONObject
原因:Jetty会导致这个问题,Tomcat可以正常启动 一.异常产生现象 使用json-lib转换实体类/字符串,跑单元测试没问题,但是启动jetty后调用JSONArray.fromObjec ...
- codeforce div 377
#include <bits/stdc++.h> using namespace std; #define pb push_back #define lb lower_bound #def ...
- html() 和 text() 方法的区别
html()方法仅支持XHTML的文档,不能用于XML文档,而text()既支持HTML文档,也 支持XML文档. html():没有参数,用于获取html的值: html(val):有参数,用于设置 ...
- C#数据库操作
1.常用的T-Sql语句 查询:SELECT * FROM tb_test WHERE ID='1' AND name='xia' SELECT * FROM ...
- java多线程学习-开篇
1.Java线程基本概念 在操作系统中两个比较容易混淆的概念是进程(process)和线程(thread).操作系统中的进程是资源的组织单位.进程有一个包含了程序内容和数据的地址空间,以及其它的资源, ...
- Java 基础知识总结 (四、String)
四.String public final class String extends Object implements Serializable, Comparable<String>, ...
- 完成了第一个java
这是我的第一个java程序,求水仙花数2016-04-26 22:39:09
- Java MD5加密工具类
public final static String MD5(String s) { char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', ' ...
- HDU 1394Minimum Inversion Number 数状数组 逆序对数量和
Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java ...