HDU 1507 Uncle Tom's Inherited Land* 二分图最大匹配

来源:互联网 发布:小说小偷源码 编辑:程序博客网 时间:2024/06/05 15:27

题意:有N*N的地,其中有些是水池,如果空地形成1*2的大小就可以卖掉。问:最多可以卖掉多少块空地?输出每块空地在N*N矩阵中的位置。

思路:(i+j)是奇数的作为X部,(i+j)是偶数的作为Y部,空格相邻就连一条线,找最大匹配即可。

http://acm.hdu.edu.cn/showproblem.php?pid=1507

/*********************************************    Problem : HDU 1507    Author  : NMfloat    InkTime (c) NM . All Rights Reserved .********************************************/#include <map>#include <set>#include <queue>#include <cmath>#include <ctime>#include <cstdio>#include <cstring>#include <cstdlib>#include <iostream>#include <algorithm>#define rep(i,a,b)  for(int i = a ; i <= b ; i ++)#define rrep(i,a,b) for(int i = b ; i >= a ; i --)#define repE(p,u) for(Edge * p = G[u].first ; p ; p = p -> next)#define cls(a,x)   memset(a,x,sizeof(a))#define eps 1e-8using namespace std;const int MOD = 1e9+7;const int INF = 0x3f3f3f3f;const int MAXN = 1e5;const int MAXE = 2e5;typedef long long LL;typedef unsigned long long ULL;int fx[] = {0,1,-1,0,0};int fy[] = {0,0,0,-1,1};struct Edge { //记录边    int to;    Edge * next;}E[MAXE],*EE;struct Gragh { //记录图的结点    Edge * first;}G[MAXN];int N,M;//二分图左右结点的个数bool visit[MAXN];int match[MAXN];//v2中匹配的情况void addedge(int u,int v) { //加边    EE->to = v ; EE -> next = G[u].first ; G[u].first = EE ++;    //EE->to = u ; EE -> next = G[v].first ; G[v].first = EE ++;}int T,n,m;void init() {    EE = E; N = M = 0;    cls(G,0);}bool find_path(int u) {    int v;    repE(p,u) {        v = p->to;        if(!visit[v]) {            visit[v] = 1;            if(match[v] == -1 || find_path(match[v])) {//v没有匹配或者v可以找到另一条路径                match[v] = u;                return true;            }        }    }    return false;}int Max_match() {    cls(match,-1);    int cnt = 0;    rep(i,1,N) {        cls(visit,0);        if(find_path(i)) cnt ++;    }    return cnt;}bool Map[105][105];int idx[105][105];int dx[105];int dy[105];void input() {    cls(Map,0);    int k;    scanf("%d",&k);    rep(i,1,k) {        int x,y;        scanf("%d %d",&x,&y);        Map[x][y] = 1;    }}void solve() {    cls(idx,0);    rep(i,1,n) rep(j,1,m) { //重新编号        if(!Map[i][j]) {            if((i+j)&1) idx[i][j] = ++N;            else idx[i][j] = ++M;        }    }    rep(i,1,n) rep(j,1,m) { //编号映射成地址        if(!Map[i][j]) {            int pos = idx[i][j];            if((i+j)&1) { dx[pos] = i; dy[pos] = j; }            else {dx[N+pos] = i ; dy[N+pos] = j; }        }    }    rep(i,1,n) rep(j,1,m) {        if(!Map[i][j]) {            if((i+j)&1) {                rep(k,1,4) {                    int tmpx = i + fx[k];                    int tmpy = j + fy[k];                    if(tmpx >= 1 && tmpx <= n && tmpy >= 1 && tmpy <= m && (!Map[tmpx][tmpy])) {                        addedge(idx[i][j],idx[tmpx][tmpy]+N);                    }                }            }        }    }    int MT = Max_match();    printf("%d\n",MT);    rep(i,N+1,N+M) {        if(match[i] + 1) {            printf("(%d,%d)--(%d,%d)\n",dx[match[i]],dy[match[i]],dx[i],dy[i]);        }    }}int main(void) {    //freopen("a.in","r",stdin);    while(scanf("%d %d",&n,&m),n+m) {        init();        input();        solve();    }    return 0;}
0 0
原创粉丝点击