poj 1486 Sorting Slides(二分图匹配的查找应用)

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

Source

Southwestern European Regional Contest 1998

给出一些矩形的坐标和一些点的坐标,若点在矩形内,则该点和该矩形匹配,问是否存在某个匹配在所有的完美匹配中,这题可以先任意找出一个完美匹配,然后依次删除该匹配的每一条边,若仍能构成完美匹配,则这个匹配不唯一,若不能构成完美匹配,则该匹配唯一

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 using namespace std;
 5 #define N 100
 6 int n;
 7 int x1[N],x2[N],y1[N],y2[N];
 8 int mp[N][N];
 9 int match[N];
10 int path[N];
11 int vis[N];
12 bool dfs(int x){
13     for(int i=0;i<n;i++){
14         if(!vis[i] && mp[x][i]){
15             vis[i]=1;
16             if(match[i]==-1 || dfs(match[i])){
17                 match[i]=x;
18                 return true;
19             }
20         }
21     }
22     return false;
23 }
24 int solve(){
25     int ans=0;
26     memset(match,-1,sizeof(match));
27
28     for(int i=0;i<n;i++){
29         memset(vis,0,sizeof(vis));
30         if(dfs(i)){
31             ans++;
32         }
33     }
34     return ans;
35 }
36 int main()
37 {
38     int ac=0;
39     while(scanf("%d",&n)==1 && n){
40             printf("Heap %d\n",++ac);
41         for(int i=0;i<n;i++){
42             scanf("%d%d%d%d",&x1[i],&x2[i],&y1[i],&y2[i]);
43         }
44         memset(mp,0,sizeof(mp));
45         for(int i=0;i<n;i++){
46             int a,b;
47             scanf("%d%d",&a,&b);
48             for(int j=0;j<n;j++){
49                 if(a>x1[j] && a<x2[j] && b>y1[j] && b<y2[j]){
50                     mp[i][j]=1;
51                 }
52             }
53         }
54
55         int ans=solve();
56         //printf("===%d\n",ans);
57         int flag=0;
58         if(ans==n){
59             for(int i=0;i<n;i++){
60                 path[i]=match[i];
61             }
62             for(int i=0;i<n;i++){
63                 int u=path[i];
64                 mp[u][i]=0;//这个表示匹配的每一条边?
65                 if(solve()==n) continue;
66                 else{
67                     if(flag)
68                       printf(" ");
69                     printf("(%c,%d)",‘A‘+i,path[i]+1);
70                     flag=1;
71                 }
72                 mp[u][i]=1;
73
74             }
75         }
76         if(!flag){
77             printf("none");
78         }
79         printf("\n\n");
80
81     }
82     return 0;
83 }

时间: 2024-10-12 03:05:33

poj 1486 Sorting Slides(二分图匹配的查找应用)的相关文章

POJ - 1486 Sorting Slides 二分图 完美匹配

题目大意:有n张透明的矩形纸张散乱在桌面上,每张纸张上面都有一个数字. 现在给出每个矩形的左下角和右上角坐标和每个数字所在的位置,问能否找出每个矩形唯一的对应数字 解题思路:分析该题可得到,二分图匹配的结果肯定是完美匹配,匹配的结果肯定为n. 接着就要判断每个点是否唯一匹配了 判断能否唯一匹配,就要在完美匹配的情况下,删除该点的那条匹配,如果还能再找出一个完美匹配,那么就表示该点表示不唯一 #include<cstdio> #include<vector> #include<

POJ 1486 Sorting Slides

http://poj.org/problem?id=1486 题意:给n个矩形的4个边界的坐标(左上和右下),分别是:xmin.xmax.ymin.ymax.又给出四个数字的坐标.每个数字只能属于一个矩形.求出每个数字的从属关系. 题解:二分图最大匹配问题:数字和矩形的匹配.要求出每一条必须边.先求出最大匹配ans,然后删除每一条边,再进行匹配,看最大匹配是否等于ans:如果相等,则这条边不是必须边:如果  小于ans,则这条边是必须边. 注意点:删除边之后要恢复这条边:输出格式-- 1 #in

POJ 1486 Sorting Slides【二分图匹配】

题目大意:有n张幻灯片和n个数字,幻灯片放置有重叠,每个数字隶属于一个幻灯片,现在问你能够确定多少数字一定属于某个幻灯片 思路:上次刷过二分图的必须点后这题思路就显然了 做一次二分匹配后将当前匹配的边删除,再做一次二分匹配,如果能匹配到那么说明这条边不是必须边 顺便说下 删边以后不用从头开始二分匹配,而是在原来二分匹配的基础上对这个点进行增广就行,另外这题格式需要注意,很容易PE #include<cstdio> #include<string.h> #include<ios

POJ 2239 Selecting Course 二分图匹配(水)

http://poj.org/problem?id=2239 题意:总共7天,每天有12个教室使用,每门课有(t,pi,qi) 表示该门课每周开t次在第qi天,第pi间教室i=1..t 总共n门课 n<=300,问最多能选多少种不同的课程? 左边点为课程 右边点为(p,q) 把(p,q)看成排列中的序数 化成整数(p-1)*12+q.求二分图的最大匹配即可 复杂度为O(nm) #include <iostream> #include <cstring> #include &l

POJ - 2594 Treasure Exploration 二分图匹配 + floyd

题目大意:在火星上有N个矿,有点矿之间存在着一条路,由于在火星比较特殊,该路变成了单向路,且机器人只能出现在这条路的两个端点,问最少需要派多少机器人,才能探清这些矿 解题思路:路可以拼接起来形成一条新的路,所以在所给的条件下还可以再扩展,用floyd将所有能连通的点找出来 接下来就是二分匹配的过程了,求出最大匹配数,在用n-最大匹配数就是答案了 #include<cstdio> #include<cstring> #include<vector> using names

poj 1469 COURSES (二分图匹配)

Language: Default COURSES Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 18290   Accepted: 7204 Description Consider a group of N students and P courses. Each student visits zero, one or more than one courses. Your task is to determine

poj 2594 Treasure Exploration 二分图匹配

点击打开链接题目链接 Treasure Exploration Time Limit: 6000MS   Memory Limit: 65536K Total Submissions: 7215   Accepted: 2947 Description Have you ever read any book about treasure exploration? Have you ever see any film about treasure exploration? Have you eve

poj 1325 Machine Schedule 二分图匹配+DFS实现

#include <stdio.h> #include <string.h> #include <iostream> #include <algorithm> #include <vector> #include <queue> #include <stack> #include <set> #include <map> #include <string> #include <ma

【图论】二分图匹配总结

二分图匹配总结 二分图匹配 1.二分图最大匹配.求两个集合内,每一个元素仅仅能用一次.两集合间存在一些匹配关系,求最大匹配多少对,利用匈牙利算法,对于每一个结点不断去找增广路去匹配 有几个重要性质: 1.最小点覆盖 = 最大匹配 2.最大独立集 = 总结点 - 最大匹配 模板: bool dfs(int u) { for (int i = 0; i < g[u].size(); i++) { int v = g[u][i]; if (vis[v]) continue; vis[v] = 1; i