【完全二分匹配必须边】POJ 1486

刚开始没看清楚题目,直接匈牙利算法,后来看到一句话Then print a series of all the slides whose numbers can be uniquely determined from the input

输出要唯一!就是问每条边是否唯一,做法:先用一次匈牙利求出一个匹配,再逐条边拆,当出现另一个完全匹配时就是none,否则可以输出结果。注意match[i] = j的i是x,j是y。

#include <map>
#include <set>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <string>
#include <cstdio>
#include <math.h>
#include <iomanip>
#include <cstdlib>
#include <limits.h>
#include <string.h>
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;

#define LL long long
#define MIN -9999999
#define MAX INT_MAX
#define pii pair<int ,int>

#define bug cout<<"here!!"<<endl
#define PI acos(-1.0)
#define FRE freopen("input.txt","r",stdin)
#define FF freopen("output.txt","w",stdout)
#define eps 1e-8
#define N 1005
int min(int a,int b){return a>b?b:a;}
int max(int a,int b){return a>b?a:b;}
bool g[N][N],vis[N];
int match[N];
int n;
int ans[N];
struct re{
    int xmin,xmax,ymin,ymax;
}p[N];
struct node{
    int x,y;
}q[N];
bool sear(int x){//匈牙利算法
    for(int i=0;i<n;i++){
        if(g[x][i] && !vis[i]){
            vis[i] = 1;
            if(match[i]== -1 || sear(match[i])){
                match[i] = x;
                return true;
            }
        }
    }
    return false;
}
bool chk(int i,int j){
    if(q[j].x>=p[i].xmin && q[j].x<=p[i].xmax && q[j].y>=p[i].ymin && q[j].y<=p[i].ymax)
    return true;
    return false;
}
void gao(){
    int i,j;
    bool ok = 0;
    for(i=0;i<n;i++){
        int tmp = match[i];
        g[tmp][i] = 0;
        match[i] = -1;
        memset(vis,0,sizeof(vis));
        if(!sear(tmp)) {
            if(ok)printf(" ");
            printf("(%c,%d)",i+'A',tmp+1);
            ok = 1;match[i] = tmp;
        }
        g[tmp][i] = 1;
    }
    if(!ok){
        puts("none\n");
    } else {
        printf("\n\n");
    }
}

int main(){
int t=1;
    while(scanf("%d",&n) && n){
        int i,j;
        memset(g,0,sizeof(g));
        for(i=0;i<n;i++){
            scanf("%d%d%d%d",&p[i].xmin,&p[i].xmax,&p[i].ymin,&p[i].ymax);
            match[i] = -1;
        }
        for(i=0;i<n;i++){
            scanf("%d%d",&q[i].x,&q[i].y);
        }
        for(i=0;i<n;i++){
            for(j=0;j<n;j++){
                if(chk(i,j)){
                    g[j][i] = 1;
                }
            }
        }
        int cnt=0;
        for(i=0;i<n;i++){
            memset(vis,0,sizeof(vis));
            if(sear(i))cnt++;
        }
        printf("Heap %d\n",t++);
        if(cnt!=n){
            puts("none");
            puts("");
        } else {
            gao();
        }
    }
    return 0;
}


你可能感兴趣的:(【完全二分匹配必须边】POJ 1486)