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. ajax请求后根据条件进行页面跳转

    $.ajx({ url: "@Url.Action("DetectCorporationCompetencyCreated", "DataBase") ...

  2. C# 队列集合的使用

    using System; using System.Collections.Generic; using System.Text; using System.Collections; namespa ...

  3. Android菜鸟成长记6 -- 网络连接的检查

    在android开发中我们要经常考虑到各种问题.在开发android应用时,涉及到要进行网络访问,时常需要进行网络状态的检查,以提供给用户必要的提醒.一般可以通过ConnectivityManager ...

  4. SAP无损耗,FP前台和回写均有2%损耗

    SAP前台显示无损耗 FP前台显示有损耗 回写也有损耗 检查:从SAP取数到FP表是没有损耗 1132物料编码的主数据也是没有损耗 检查:FP_MO2SAP存储过程

  5. Asp.net Session 保存到MySql中

    一 网站项目引入"mysql.web.dll" 二 web.config配置中添加mysql数据库连接字符串 <connectionStrings> <remov ...

  6. (五) 一起学 Unix 环境高级编程 (APUE) 之 进程环境

    . . . . . 目录 (一) 一起学 Unix 环境高级编程 (APUE) 之 标准IO (二) 一起学 Unix 环境高级编程 (APUE) 之 文件 IO (三) 一起学 Unix 环境高级编 ...

  7. JDBC第一天

    (一)JDBC访问数据库的工作过程:               <1>加载驱动,建立连接 oracle数据库:在项目中导入ojdbc.jar包 数据库驱动路径:String driver ...

  8. mongodb:短网址项目

    短网址项目概述 1.短网址项目,是将给定的长网址,转换成短网址. 如 新浪 http://t.cn/zQd5NPw ,其中zQd5NPw就是短网址 前段页面如下

  9. Scrum会议10.20

    Scrum会议   组名称:好好学习 项目名称:记账本 参会成员:林莉(Master)胡丽娜 汪东涵 宫丽君 时间:2016.10.20 已完成内容: 1.理解项目和代码. 2.讨论新功能. 计划完成 ...

  10. nginx解析php请求为404

    刚搭建了lnmp环境,发现如果讲我的程序放在某个文件夹的时候,然后nginx进行代理的时候竟然会是404: nginx配置如下: # pass the PHP scripts to FastCGI s ...