Description

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

 

题目大意:给你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(挑选幻灯片,二分匹配,中等)的更多相关文章

  1. POJ 1486 Sorting Slides(二分图完全匹配必须边)题解

    题意:给你n张照片的范围,n个点的坐标,问你能唯一确定那几个点属于那几张照片,例如样例中4唯一属于A,2唯一属于C,1唯一属于B,3唯一属于C 思路:进行二分图完全匹配,怎么判断唯一属于?匹配完之后删 ...

  2. [ACM_图论] The Perfect Stall 完美的牛栏(匈牙利算法、最大二分匹配)

    描述 农夫约翰上个星期刚刚建好了他的新牛棚,他使用了最新的挤奶技术.不幸的是,由于工程问题,每个牛栏都不一样.第一个星期,农夫约翰随便地让奶牛们进入牛栏,但是问题很快地显露出来:每头奶牛都只愿意在她们 ...

  3. UVA663 Sorting Slides(烦人的幻灯片)

    UVA663 Sorting Slides(烦人的幻灯片) 第一次做到这么玄学的题,在<信息学奥赛一本通>拓扑排序一章找到这个习题(却发现标程都是错的),结果用二分图匹配做了出来 蒟蒻感觉 ...

  4. POJ 1486 Sorting Slides【二分图匹配】

    题目大意:有n张幻灯片和n个数字,幻灯片放置有重叠,每个数字隶属于一个幻灯片,现在问你能够确定多少数字一定属于某个幻灯片 思路:上次刷过二分图的必须点后这题思路就显然了 做一次二分匹配后将当前匹配的边 ...

  5. poj 1486 Sorting Slides

    Sorting Slides Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 4469   Accepted: 1766 De ...

  6. 【HDU 2255】奔小康赚大钱 (最佳二分匹配KM算法)

    奔小康赚大钱 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Subm ...

  7. [置顶] 白话二分匹配之最大匹配+附上hdu2063解题报告

    最近开始学习图论的二分匹配,关于最大匹配做一次小总结,希望自己后面回头来看一目明了,也对刚接触的人有帮助: ps:开始有的文字很多....对于很多人来说一看到文字就烦啦...不过这个总结是针对匈牙利算 ...

  8. POJ1468 Sorting Slides

    Sorting Slides Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 4442   Accepted: 1757 De ...

  9. POJ 1486 Sorting Slides (KM)

    Sorting Slides Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 2831   Accepted: 1076 De ...

随机推荐

  1. require和include的区别

    require 的使用方法如 require("MyRequireFile.php"); .这个函数通常放在 PHP 程序的最前面,PHP 程序在执行前,就会先读入 require ...

  2. dubbo properties

    DUBBO配置项的优先级: java -D优先于 Spring配置,Spring配置优先于 properties文件的配置,这也符合一般项目的规则. 覆盖策略: JVM启动-D参数优先,这样可以使用户 ...

  3. 调试CS5343总结报告

    一周前接到新任务,调试CS5343,百度一下,CS5343是一款音频采集的AD芯片,CS5343驱动代码是现成的,我的工作是提高芯片的采样速率,看了一边芯片的Datesheet,辛好东西不是很多. 概 ...

  4. eclipse4.x 启动之后, "Initializing Java Tooling" 卡住问题解决

    eclipse4.x 启动之后, "Initializing Java Tooling(1%)",其他操作均被阻塞,导致无法正常工作, 解决方案: 删除当前工作目录下的worksp ...

  5. [题解]hdu 1009 FatMouse' Trade(贪心基础题)

    Problem Description FatMouse prepared M pounds of cat food, ready to trade with the cats guarding th ...

  6. Train Problem I hdu 1022(栈)

    http://acm.split.hdu.edu.cn/showproblem.php?pid=1022 题意:给出火车的进站与出站顺序,判断是否可以按照给出的出站顺序出站. #include < ...

  7. ListView和ScrollView冲突

    当ListView放在ScrollView中的时候,无论你设置高度为 match_parent(填充父窗体)和wrap_content(包裹内容)都只显示一行,这是你把ListView放在Linear ...

  8. server 2008 IIS 搭建PHP运行环境

    本文以windows server 2008 r2 Enterprise作为操作系统,以IIS为web部署服务组件,配置PHP的服务器端执行环境,其中IIS版本为7.5,PHP版本为5.3. 注意:本 ...

  9. JavaScript parseInt() toString()函数

    parseInt(string, radix) string:必需.要被解析的字符串 radix:可选.表示要解析的数字的基数.该值介于 2 ~ 36 之间. 如果省略该参数或其值为 0,则数字将以 ...

  10. ngxtop:在命令行实时监控 Nginx 的神器

    Nginx网站服务器在生产环境中运行的时候需要进行实时监控.实际上,诸如Nagios, Zabbix, Munin 的网络监控软件是支持 Nginx 监控的. 如果你不需要以上软件提供的综合性报告或者 ...