1. 程式人生 > >poj1486二分匹配

poj1486二分匹配

ret find match include owin amp should 分享 mit

Sorting Slides
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 4777 Accepted: 1867

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

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


貪心WA 二分又WA一次

5
1 4 1 4
2 7 2 7
5 11 5 11
8 13 9 14
9 14 8 13
3 3
3 3
6 6
10 10
12 12

結果應該是(C,3),但貪心的結果是none
#include<vector>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=33;
int used[N],x[N],y[N];
bool vis[N],mp[N][N];
vector<int>G[N];
struct node
{
    int xi,xx,yi,yx;
} e[N];
struct point
{
    int x,y;
    bool operator < (const point &A)const
    {
        return x<A.x;
    }
} al[N];
bool findx(int x)
{
    for(int i=0; i<(int)G[x].size(); ++i)
    {
        int v=G[x][i];
        if(vis[v]||mp[x][v]) continue;
        vis[v]=1;
        if(!used[v]||findx(used[v]))
        {
            used[v]=x;
            return true;
        }
    }
    return false;
}
int ctx[N],cty[N];
int main()
{
    int n,tas=1;
    while(scanf("%d",&n),n)
    {
        for(int i=1; i<=n; ++i) G[i].clear();
        for(int i=1; i<=n; ++i) scanf("%d%d%d%d",&e[i].xi,&e[i].xx,&e[i].yi,&e[i].yx);
        for(int i=1; i<=n; ++i) scanf("%d%d",&x[i],&y[i]);
        for(int i=1; i<=n; ++i) for(int j=1; j<=n; ++j) if(e[i].xi<=x[j]&&x[j]<=e[i].xx&&e[i].yi<=y[j]&&e[i].yx>=y[j]) G[i].push_back(j);
        memset(used,0,sizeof(used));
        memset(mp,0,sizeof(mp));
        int tot=0,maxmatch=0,fx=0;
        for(int i=1; i<=n; ++i)
        {
            memset(vis,0,sizeof(vis));
            if(findx(i)) ++maxmatch;
        }
        for(int i=1; i<=n; ++i) if(used[i]) ctx[tot]=used[i],cty[tot++]=i;
        printf("Heap %d\n",tas++);
        if(!tot) puts("none");
        else
        {
/*for(int i=0; i<tot; ++i)
{
mp[ctx[i]][cty[i]]=1;
memset(vis,0,sizeof(vis));
used[cty[i]]=0;
if(!findx(ctx[i])) al[fx].x=ctx[i],al[fx++].y=cty[i];
mp[ctx[i]][cty[i]]=0;
used[cty[i]]=ctx[i];
}這麽寫不對的*/
for(int i=0; i<tot; ++i) { int now=0; mp[ctx[i]][cty[i]]=1; memset(used,0,sizeof(used)); for(int i=1; i<=n; ++i) { memset(vis,0,sizeof(vis)); if(findx(i)) ++now; } if(now!=maxmatch) al[fx].x=ctx[i],al[fx++].y=cty[i]; mp[ctx[i]][cty[i]]=0; } if(!fx) puts("none"); else { sort(al,al+fx); for(int i=0; i<fx; ++i) printf("(%c,%d) ",al[i].x+A-1,al[i].y); puts(""); } } puts(""); } }

poj1486二分匹配