POJ 1486 Sorting Slides(二分图最大匹配:关键边)

来源:互联网 发布:玲珑醉梦网络剧百度云 编辑:程序博客网 时间:2024/06/06 00:18

题意:一些幻灯片,有一些数字在幻灯片里面,看能不能使得一个数字只能对应一张幻灯片.第一行代表有几张幻灯片.

给出这几张幻灯片的坐标,接着n行代表1-n个数字对应的坐标,然后要求你打印一个确定的使一个数字对应一张幻灯片.

如果不能对应输出none.存在某些幻灯片只能由某个数字代表的话就按字典序打印出这些幻灯片和对应的数字.

思路二分图最大匹配问题.左边点集用幻灯片编号表示,右边点集用数字表示. 如果某个幻灯片i包含了数字j,那么从左边i到右边j就存在一条边.首先我们求出这个图的最大匹配数x, 根据题意这x值一定是等于n(幻灯片数的). 然后我们记录目前求到的最大匹配的各个边.我们每次判断最大匹配边集的某条边是否是必需边. 我们只要先删除这条边,如果之后求最大匹配数依然==n,那么这条边不是必需边.如果之后求最大匹配数依然<n,那么这条边是必需边.(做好标记)最终我们只需要输出所有的必须边即可.


#include<cstdio>#include<cstring>#include<vector>using namespace std;const int maxn=500+10;struct Max_Match{    int n,m;    int g[maxn][maxn];    bool vis[maxn];    int left[maxn];    void init(int n)    {        this->n=n;    //    this->m=m;        memset(left,-1,sizeof(left));memset(g,0,sizeof(g));    }    bool match(int u)    {        for(int v=1;v<=n;v++)        {            if(g[u][v] && !vis[v])            {                vis[v]=true;                if(left[v]==-1 || match(left[v]))                {                    left[v]=u;                    return true;                }            }        }        return false;    }    int solve()    {        int ans=0;        for(int i=1;i<=n;i++)        {            memset(vis,0,sizeof(vis));            if(match(i)) ans++;        }        return ans;    }}MM;int T;int cas = 1;int xmin[maxn],ymin[maxn],xmax[maxn],ymax[maxn];struct Node{int x;bool ok;}node[maxn];int main(){    int n;    while (scanf("%d",&n)!=EOF && n){MM.init(n);memset(node,0,sizeof(node));for (int i = 1;i<=n;i++){scanf("%d%d%d%d",&xmin[i],&xmax[i],&ymin[i],&ymax[i]);}for (int i = 1;i<=n;i++){int x,y;scanf("%d%d",&x,&y);for (int j = 1;j<=n;j++){if (xmin[j]<=x && x<=xmax[j] && ymin[j]<=y && y<=ymax[j])MM.g[j][i]=1;}}MM.solve();int num = n;for (int i = 1;i<=n;i++){node[MM.left[i]].x=i;node[MM.left[i]].ok=1;}for (int i = 1;i<=n;i++){int j = node[i].x;MM.g[i][j]=0;memset(MM.left,-1,sizeof(MM.left));int temp = MM.solve();if (temp == n){node[i].ok=0;num--;}MM.g[i][j]=1;}printf("Heap %d\n",cas++);if (num==0)printf("none\n");else{for (int i = 1;i<=n;i++)if (node[i].ok)printf("(%c,%d) ",i-1+'A',node[i].x);printf("\n");}printf("\n");}    return 0;}

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

46 22 10 204 18 6 168 20 2 1810 24 4 89 1519 1711 721 1120 2 0 20 2 0 21 11 10

Sample Output

Heap 1(A,4) (B,1) (C,2) (D,3)Heap 2none


0 0
原创粉丝点击