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个矩阵的左上角和右下角坐标,以及n个点的坐标,让每个矩阵阵对应一个点,求出完全确定的对应情况,并输出,如果没有一个完全对应的就输出none。
解题思路:二分匹配问题,涉及必备边(本人自己命名)。首先在每个矩阵都会有一个点与之对应的情况下,确定那些对应关系是否是确定的,只需枚举每一条对应关系,将其关系在图中删去,进行匹配,如果最大匹配数还是和原先的相等,不就是说明了该对应关系没必要存在,反之就是必与存在的对应关系。再把确定后的对应关系按照顺序输出就OK了。
代码:
#include<stdio.h>
#include<string.h>
int a[1010][1010];
int b[1010];
int v[1010];
int c[1010][4];
int n;
int pan(int x,int y,int i)
{
if(x>=c[i][0]&&x<=c[i][1]&&y>=c[i][2]&&y<=c[i][3])
return 1;
else
return 0;
}
int dfs(int u)
{
int i;
for(i=1;i<=n;i++)
{
if(!b[i]&&a[u][i])
{
b[i]=1;
if(!v[i]||dfs(v[i]))
{
v[i]=u;
return 1;
}
}
}
return 0;
}
int main()
{
int i,j,k,x,y,t;
int d[510][2];
int f[510][2];
int g[510];
t=0;
while(scanf("%d",&n),n!=0)
{
t++;
for(i=1;i<=n;i++)
scanf("%d%d%d%d",&c[i][0],&c[i][1],&c[i][2],&c[i][3]);
for(i=1;i<=n;i++)
scanf("%d%d",&d[i][0],&d[i][1]);
memset(a,0,sizeof(a));
memset(v,0,sizeof(v));
memset(f,0,sizeof(f));
memset(g,0,sizeof(g));
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(pan(d[i][0],d[i][1],j))
a[i][j]=1;
}
}
k=0;
for(i=1;i<=n;i++)
{
memset(b,0,sizeof(b));
if(dfs(i))
k++;
}
for(i=1;i<=n;i++)
{
f[i][0]=i;
f[i][1]=v[i];
}
int m=n;
for(j=1;j<=n;j++)
{
a[f[j][1]][f[j][0]]=0;
k=0;
memset(v,0,sizeof(v));
for(i=1;i<=n;i++)
{
memset(b,0,sizeof(b));
if(dfs(i))
k++;
}
if(k==n)
{
m--;
g[f[j][0]]=1;
}
a[f[j][1]][f[j][0]]=1;
}
printf("Heap %d\n",t);
if(m==0)
printf("none\n");
else
{
for(i=1;i<=n;i++)
if(!g[i])
{
printf("(%c,%d)",f[i][0]+'A'-1,f[i][1]);
break;
}
for(j=i+1;j<=n;j++)
if(!g[j])
{
printf(" (%c,%d)",f[j][0]+'A'-1,f[j][1]);
}
printf("\n");
}
printf("\n");
}
return 0;
}