POJ 1486 Sorting Slides 求二分图的必须边

来源:互联网 发布:信捷plc读取485数据 编辑:程序博客网 时间:2024/06/05 09:50
点击打开链接

Sorting Slides
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 3077 Accepted: 1188

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

Source

Southwestern European Regional Contest 1998

给你n张幻灯片的坐标编号分别为A,B,C......然后给你n个数字的坐标,已知每个数字对应一张幻灯片,让你输出哪些数字和幻灯片的对应关系是必须的。
先求出原来的最大匹配数,然后枚举每一条边并删除再求最大匹配数,如果删边之后得到的最大匹配数比原来的最大匹配数小,那么这条边就是一条必须边。
//4340K32MS#include<stdio.h>#include<string.h>#define M 1007int link[M],g[M][M];bool vis[M];int n,count;int x[M],y[M];struct E{    int x1,y1,x2,y2;}s[M];bool find(int x){    for(int i=1;i<=n;i++)        if(!vis[i]&&g[x][i])        {            vis[i]=true;            if(!link[i]||find(link[i]))            {                link[i]=x;                return true;            }        }    return false;}int solve(){    count=0;    memset(link,0,sizeof(link));    for(int i=1;i<=n;i++)        {            memset(vis,false,sizeof(vis));            if(find(i))                count++;        }    return count;}int main(){    int cas=1;    while(scanf("%d",&n),n)    {        memset(g,0,sizeof(g));        for(int i=1;i<=n;i++)            scanf("%d%d%d%d",&s[i].x1,&s[i].x2,&s[i].y1,&s[i].y2);        for(int i=1;i<=n;i++)        {            scanf("%d%d",&x[i],&y[i]);            for(int j=1;j<=n;j++)//建图                if(x[i]>=s[j].x1&&x[i]<=s[j].x2&&y[i]>=s[j].y1&&y[i]<=s[j].y2)//如果数字的坐标在幻灯片之内                    g[i][j]=1;        }        printf("Heap %d\n",cas++);        int ans=solve();//求出原来的最大匹配数        bool flag=false;        for(int j=1;j<=n;j++)            for(int i=1;i<=n;i++)            {                if(!g[i][j])continue;                g[i][j]=0;//如果原来连接,则删除此边                if(solve()<ans)//如果删除这条边之后,最大匹配数减少了                {                    flag=true;                    printf("(%c,%d) ",'A'+j-1,i);//说明这条边为二分匹配图的必须边                }                g[i][j]=1;//重新连接刚刚删除的这条边            }        if(!flag)printf("none\n\n");        else printf("\n\n");    }    return 0;}


0 0
原创粉丝点击