Sorting Slides(二分图匹配——确定唯一匹配边)
题目描述:
Professor Clumsey is going to give an important talk this afternoon. Unfortunately, he is not a very tidy person and has put all his transparencies on one big heap. Before giving the talk, he has to sort the slides. Being a kind of minimalist, he wants to do this with the minimum amount of work possible.
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
/*
题意描述:输入幻灯片的坐标位置,页码的坐标位置,将能唯一确定页码的幻灯片按照顺序输出,
如果没有一张能够唯一确定则输出none
*/
/*
解题思路:基本思路是将幻灯片和页码进行匹配,按照二分图匹配的思路求最大匹配,如果某一条边
是唯一边,则将这条边去掉以后,该二分图的最大匹配数将<n,输出该匹配边。
*/
#include<stdio.h>
#include<string.h>
const int N=;
struct Slide{
int xmin,xmax,ymin,ymax;
}slide[N];
struct Num{
int x,y;
}num[N];
int n,e[N][N],cx[N],cy[N],bk[N];
int maxmatch();
int path(int u); int main()
{
int i,j,t=;
while(scanf("%d",&n), n != )
{
for(i=;i<n;i++)
scanf("%d%d%d%d",&slide[i].xmin,&slide[i].xmax,&slide[i].ymin,&slide[i].ymax);
for(i=;i<n;i++)
scanf("%d%d",&num[i].x,&num[i].y); memset(e,,sizeof(e));
for(i=;i<n;i++){
for(j=;j<n;j++){
if(num[i].x>slide[j].xmin && num[i].x<slide[j].xmax
&& num[i].y>slide[j].ymin && num[i].y<slide[j].ymax)
e[j][i]=;//点i在j张里,按照输出顺序,先输出字母,再输出数字
}
} printf("Heap %d\n",++t);
int flag=;
for(i=;i<n;i++){
for(j=;j<n;j++){
if(e[i][j])
{
e[i][j]=;
if(maxmatch() < n)
{
if(flag)
printf(" (%c,%d)",'A'+i,j+);
else
printf("(%c,%d)",'A'+i,j+);
flag=;
}
e[i][j]=;
}
}
} if(flag)
printf("\n\n");
else
printf("none\n\n");
}
return ;
} int maxmatch()
{
int i;
memset(cx,-,sizeof(cx));
memset(cy,-,sizeof(cy));
int ans=;
for(i=;i<n;i++)
{
if(cy[i]==-)
{
memset(bk,,sizeof(bk));
ans += path(i);
}
}
return ans;
}
int path(int u)
{
int i;
for(i=;i<n;i++)
{
if(e[u][i] && !bk[i])
{
bk[i]=;
if(cx[i]==- || path(cx[i]))
{
cx[i]=u;
cy[u]=i;
return ;
}
}
}
return ;
}
/*易错分析:按照输出顺序,先幻灯片再页码,所以输入边的时候j在前,i在后*/
Sorting Slides(二分图匹配——确定唯一匹配边)的更多相关文章
- poj 1486 Sorting Slides(二分图匹配的查找应用)
Description Professor Clumsey is going to give an important talk this afternoon. Unfortunately, he i ...
- [ACM_图论] Sorting Slides(挑选幻灯片,二分匹配,中等)
Description Professor Clumsey is going to give an important talk this afternoon. Unfortunately, he i ...
- POJ 1486 Sorting Slides (二分图关键匹配边)
题意 给你n个幻灯片,每个幻灯片有个数字编号1~n,现在给每个幻灯片用A~Z进行编号,在该幻灯片范围内的数字都可能是该幻灯片的数字编号.问有多少个幻灯片的数字和字母确定的. 思路 确定幻灯片的数字就是 ...
- POJ 1486 Sorting Slides(二分图匹配)
[题目链接] http://poj.org/problem?id=1486 [题目大意] 给出每张幻灯片的上下左右坐标,每张幻灯片的页码一定标在这张幻灯片上, 现在问你有没有办法唯一鉴别出一些幻灯片 ...
- 【POJ】1486:Sorting Slides【二分图关键边判定】
Sorting Slides Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 5390 Accepted: 2095 De ...
- UVa 1349 (二分图最小权完美匹配) Optimal Bus Route Design
题意: 给出一个有向带权图,找到若干个圈,使得每个点恰好属于一个圈.而且这些圈所有边的权值之和最小. 分析: 每个点恰好属于一个有向圈 就等价于 每个点都有唯一后继. 所以把每个点i拆成两个点,Xi ...
- 二分图匹配之最佳匹配——KM算法
今天也大致学了下KM算法,用于求二分图匹配的最佳匹配. 何为最佳?我们能用匈牙利算法对二分图进行最大匹配,但匹配的方式不唯一,如果我们假设每条边有权值,那么一定会存在一个最大权值的匹配情况,但对于KM ...
- UVA 1349 Optimal Bus Route Design (二分图最小权完美匹配)
恰好属于一个圈,那等价与每个点有唯一的前驱和后继,这让人想到了二分图, 把一个点拆开,点的前驱作为S集和点的后继作为T集,然后连边,跑二分图最小权完美匹配. 写的费用流..最大权完美匹配KM算法没看懂 ...
- 紫书 例题11-10 UVa 1349 (二分图最小权完美匹配)
二分图网络流做法 (1)最大基数匹配.源点到每一个X节点连一条容量为1的弧, 每一个Y节点连一条容量为1的弧, 然后每条有向 边连一条弧, 容量为1, 然后跑一遍最大流即可, 最大流即是最大匹配对数 ...
随机推荐
- flexbox 弹性盒子
flexbox 弹性盒子 1.基本知识 container(容器)属性 flex-direction: row | row-reverse | column | column-reverse 属性决定 ...
- PL/SQL 实现行列转换
这篇博文写的是简单的行列转换的,以一个具体的例子来给出. 以前在论坛上有人问过相关的问题,上面的回答五光十色,有很多是可行的,当然更多的是自以为很高端,实际却不着边际的回答.下面进入正题. part1 ...
- centos6+cdh5.4.0 离线搭建cdh搭建
p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...
- 打造自己的 JavaScript 武器库(转自SegmentFault公众号)
2017-11-14 SlaneYang SegmentFault 自己打造一把趁手的武器,高效率完成前端业务代码. 前言 作为战斗在业务一线的前端,要想少加班,就要想办法提高工作效率.这里提一个小点 ...
- Spark算子--SortByKey
转载请标明出处http://www.cnblogs.com/haozhengfei/p/076a31e7caab1316b07990c02ac65e9c.html SortByKey--Transf ...
- asp.net网站管理工具 遇到错误。请返回上一页并重试。
原因:项目的路径里有“#”号.
- 从CUMT校园导航出现的问题看CSS布局设计(一) CSS盒模型
先说说做的这个校园导航系统值得一提的内容: 1. 二级菜单栏 .iframe内嵌窗口(样式设计.用hover做效果) 2. 高德地图API (自定义底图样式.弹跳点.信息窗体.线路导航) 3. DO ...
- util包就是用来放一些公用方法和数据结构的
util包就是用来放一些公用方法和数据结构的
- CCF系列之最大的矩形(201312-3)
试题名称: 最大的矩形 时间限制: 1.0s 内存限制: 256.0MB 问题描述: 问题描述 在横轴上放了n个相邻的矩形,每个矩形的宽度是1,而第i(1 ≤ i ≤ n)个矩形的高度是hi.这n个矩 ...
- requests关于Exceeded 30 redirects问题得出的结论
昨天一个朋友在爬网页时出现的一个问题,以及后续我对这个问题进行了简单的测试. 先说出现的问题的简单描述. 首先是使用urllib请求网页: #urllib.request发起的请求 import ur ...