POJ2446_Chessboard(二分图最大匹配)

解题报告

http://blog.csdn.net/juncoder/article/details/38172083

题目传送门

题意:

M×N的矩阵,k个点被标记,用2×1的木板最多能够放置多少个。

思路:

把标记的格子除外,链接相邻的两个格子,然后最大匹配出来的是二分图的两倍。

c++TLE了,G++1700+过了,理论上匈牙利算法的时间复杂度是n^3。就应该超时。可能数据弱吧。

另一种建图方式就是建成二分图,将矩阵中的点奇偶分。

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
int n,m,k,mmap[2050][2050],edge[2050][2050],pre[2050],vis[2050],kk;
int dx[]= {1,-1,0,0};
int dy[]= {0,0,1,-1};
int dfs(int x)
{
    for(int i=1; i<=kk; i++) {
        if(!vis[i]&&edge[x][i]) {
            vis[i]=1;
            if(pre[i]==-1||dfs(pre[i])) {
                pre[i]=x;
                return 1;
            }
        }
    }
    return 0;
}
int main()
{
    int i,j,a,b;
    while(~scanf("%d%d%d",&m,&n,&k)) {
        memset(mmap,0,sizeof(mmap));
        memset(pre,-1,sizeof(pre));
        memset(edge,0,sizeof(edge));
        for(i=1; i<=k; i++) {
            scanf("%d%d",&b,&a);
            mmap[a][b]=-1;
        }
        if(n*m%2!=k%2) {
            printf("NO
");
            continue;
        }
        kk=0;
        for(i=1; i<=m; i++) {
            for(j=1; j<=n; j++) {
                if(!mmap[i][j])
                    mmap[i][j]=++kk;
            }
        }
        if(kk%2!=0) {
            printf("NO
");
            continue;
        }
        int l=0;
        for(i=1; i<=m; i++) {
            for(j=1; j<=n; j++) {
                if(mmap[i][j]!=-1)
                    for(l=0; l<4; l++) {
                        int x=i+dx[l];
                        int y=j+dy[l];
                        if(x>=1&&x<=m&&y>=1&&y<=n&&mmap[x][y]!=-1) {
                            edge[mmap[i][j]][mmap[x][y]]=1;
                        }
                    }
            }
        }
        int ans=0;
        for(i=1; i<=kk; i++) {
            memset(vis,0,sizeof(vis));
            ans+=dfs(i);
        }
        if(ans==kk)
            printf("YES
");
        else printf("NO
");
    }
    return 0;
}


Chessboard
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 13140   Accepted: 4105

Description

Alice and Bob often play games on chessboard. One day, Alice draws a board with size M * N. She wants Bob to use a lot of cards with size 1 * 2 to cover the board. However, she thinks it too easy to bob, so she makes some holes on the board (as shown in the figure below). 

We call a grid, which doesn’t contain a hole, a normal grid. Bob has to follow the rules below: 
1. Any normal grid should be covered with exactly one card. 
2. One card should cover exactly 2 normal adjacent grids. 

Some examples are given in the figures below: 
 
A VALID solution.

 
An invalid solution, because the hole of red color is covered with a card.

 
An invalid solution, because there exists a grid, which is not covered.

Your task is to help Bob to decide whether or not the chessboard can be covered according to the rules above.

Input

There are 3 integers in the first line: m, n, k (0 < m, n <= 32, 0 <= K < m * n), the number of rows, column and holes. In the next k lines, there is a pair of integers (x, y) in each line, which represents a hole in the y-th row, the x-th column.

Output

If the board can be covered, output "YES". Otherwise, output "NO".

Sample Input

4 3 2
2 1
3 3

Sample Output

YES

Hint

 
A possible solution for the sample input.

Source

POJ Monthly,charlescpp
原文地址:https://www.cnblogs.com/wgwyanfs/p/7368031.html