Sorting Slides
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 4176   Accepted: 1619

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.
<center> </center>
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


题意:rt

做法:记录之前的路径,设每条边不存在,如果最大匹配数仍是n,那么这个边不是必须边

注意 xy不要弄反了

#include <iostream>
#include <stdio.h>
#include <string.h>
#define M 1000
#define inf 0x3f3f3f3f
using namespace std;
const int MAXN=1000;
int uN,vN;//u,v数目
int g[MAXN][MAXN];
int linker[MAXN];
bool used[MAXN];
bool dfs(int u)//从左边开始找增广路径
{
    int v;
    for(v=1;v<=vN;v++)//这个顶点编号从0开始,若要从1开始需要修改
      if(g[u][v]&&!used[v])
      {
          used[v]=true;
          if(linker[v]==-1||dfs(linker[v]))
          {//找增广路,反向
              linker[v]=u;
              return true;
          }
      }
    return false;//这个不要忘了,经常忘记这句
}
int hungary()
{
    int res=0;
    int u;
    memset(linker,-1,sizeof(linker));
    for(u=1;u<=uN;u++)
    {
        memset(used,0,sizeof(used));
        if(dfs(u))res++;
    }
    return res;
}
struct node1
{
    int xmin, xmax, ymin ,ymax;
}num1[M];
struct node2
{
    int x,y;
}num2[M];
int path[M];
int main()
{
   // freopen("cin.txt","r",stdin);
    int n,cas=1;
    while(~scanf("%d",&n)&&n)
    {
        vN=uN=n;
        for(int i=1;i<=n;i++)scanf("%d%d%d%d",&num1[i].xmin,&num1[i].xmax,&num1[i].ymin,&num1[i].ymax);
        for(int i=1;i<=n;i++)scanf("%d%d",&num2[i].x,&num2[i].y);
        memset(g,0,sizeof(g));
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n;j++)
            {
                if(num2[j].x<num1[i].xmax&&num2[j].x>num1[i].xmin&&num2[j].y<num1[i].ymax&&num2[j].y>num1[i].ymin)
                {
                   // num[cnt].a=i;num[cnt].b=j;cnt++;
                    g[j][i]=1;
                }
            }
        }
        int sum=hungary();
       // printf("sum=%d\n",sum);
        bool fl=0;
        printf("Heap %d\n",cas++);
        if(sum==n)
        {
            for(int i=1;i<=n;i++)path[i]=linker[i];//保存最大情况下的匹配关系
            for(int i=1;i<=n;i++)
            {
                g[path[i]][i]=0;//将这个匹配关系设成0
                if(hungary()==n)continue;
                else
                {
                    if(fl)printf(" ");
                    printf("(%c,%d)",'A'+i-1,path[i]);
                    fl=1;
                }
                g[path[i]][i]=1;//还原
            }
        }
        if(!fl)printf("none");
        puts("");
        puts("");
    }
    return 0;
}