1. 程式人生 > >Sorting Slides(二分圖匹配——確定唯一匹配邊)

Sorting Slides(二分圖匹配——確定唯一匹配邊)

win 數字 his mem sam tps nes coo 描述

題目描述:

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

  1 /*
  2 題意描述:輸入幻燈片的坐標位置,頁碼的坐標位置,將能唯一確定頁碼的幻燈片按照順序輸出,
  3 如果沒有一張能夠唯一確定則輸出none
  4 */ 
  5 /*
  6 解題思路:基本思路是將幻燈片和頁碼進行匹配,按照二分圖匹配的思路求最大匹配,如果某一條邊
  7 是唯一邊,則將這條邊去掉以後,該二分圖的最大匹配數將<n,輸出該匹配邊。
  8 */ 
  9 #include<stdio.h>
 10 #include<string.h>
 11 const int N=501;
 12 struct Slide{
 13     int xmin,xmax,ymin,ymax;
 14 }slide[N];
 15 struct Num{
 16     int x,y;
 17 }num[N];
 18 int n,e[N][N],cx[N],cy[N],bk[N]; 
 19 int maxmatch();
 20 int path(int u);
 21 
 22 int main()
 23 {
 24     int i,j,t=0;
 25     while(scanf("%d",&n), n != 0)
 26     {
 27         for(i=0;i<n;i++)
 28             scanf("%d%d%d%d",&slide[i].xmin,&slide[i].xmax,&slide[i].ymin,&slide[i].ymax);
 29         for(i=0;i<n;i++)
 30             scanf("%d%d",&num[i].x,&num[i].y);
 31             
 32         memset(e,0,sizeof(e));
 33         for(i=0;i<n;i++){
 34             for(j=0;j<n;j++){
 35                 if(num[i].x>slide[j].xmin && num[i].x<slide[j].xmax 
 36                 && num[i].y>slide[j].ymin && num[i].y<slide[j].ymax)
 37                 e[j][i]=1;//點i在j張裏,按照輸出順序,先輸出字母,再輸出數字 
 38             }
 39         }    
 40         
 41         printf("Heap %d\n",++t); 
 42         int flag=0;
 43         for(i=0;i<n;i++){
 44             for(j=0;j<n;j++){
 45                 if(e[i][j])
 46                 {
 47                     e[i][j]=0;
 48                     if(maxmatch() < n)
 49                     {
 50                         if(flag)
 51                         printf(" (%c,%d)",A+i,j+1); 
 52                         else 
 53                         printf("(%c,%d)",A+i,j+1); 
 54                         flag=1;
 55                     }
 56                     e[i][j]=1;
 57                 }
 58             }
 59         }
 60              
 61         if(flag)
 62         printf("\n\n");
 63         else
 64         printf("none\n\n");
 65     }
 66     return 0;
 67 }
 68 
 69 int maxmatch()
 70 {
 71     int i;
 72     memset(cx,-1,sizeof(cx));
 73     memset(cy,-1,sizeof(cy));
 74     int ans=0;
 75     for(i=0;i<n;i++)
 76     {
 77         if(cy[i]==-1)
 78         {
 79             memset(bk,0,sizeof(bk));
 80             ans += path(i);
 81         }    
 82     } 
 83     return ans;
 84 } 
 85 int path(int u)
 86 {
 87     int i;
 88     for(i=0;i<n;i++)
 89     {
 90         if(e[u][i] && !bk[i])
 91         {
 92             bk[i]=1; 
 93             if(cx[i]==-1 || path(cx[i]))
 94             {
 95                 cx[i]=u;
 96                 cy[u]=i;
 97                 return 1;
 98             }
 99         }
100     }
101     return 0;
102 }
103 /*易錯分析:按照輸出順序,先幻燈片再頁碼,所以輸入邊的時候j在前,i在後*/ 

Sorting Slides(二分圖匹配——確定唯一匹配邊)