Poj-2446 Chessboard 二分匹配
2014-07-14 14:42
393 查看
#include <stdio.h> #include <string.h> #include <math.h> #include <iostream> #include <algorithm> using namespace std; typedef long long LL; const int maxn = 105; const int M = 50*50; const int Mod = 1000000007; int n,m,K; int nx,ny; int id[maxn][maxn]; int map[M][M]; int cx[M],cy[M]; bool vis[M]; bool mark[maxn][maxn]; int xs[] = { 0,1,0,-1 }; int ys[] = { 1,0,-1,0 }; int findpath( int u ) { int i,j; for( i = 1; i <= ny; i++ ) { if( map[u][i] && !vis[i] ) { vis[i] = 1; if( cy[i] == -1 || findpath( cy[i] ) ) { cy[i] = u; cx[u] = i; return true; } } } return false; } int MaxMatch() { int ans = 0; int i,j; memset(cx,-1,sizeof(cx)); memset(cy,-1,sizeof(cy)); for( i = 1; i <= nx; i ++ ) { if( cx[i] == -1 ) { memset(vis,0,sizeof(vis)); ans += findpath(i); } } return ans; } int main() { #ifndef ONLINE_JUDGE freopen("data.txt","r",stdin); #endif int a,b,xx,yy; while( scanf("%d%d%d",&n,&m,&K) != EOF ) { nx = ny = 0; for( int i = 0; i < K; i ++ ) { scanf("%d%d",&b,&a); mark[a][b] = 1; } for( int i = 1;i <= n; i ++ ) { for( int j = 1; j <= m; j ++ ) { if( (i+j)%2 == 0 ) id[i][j] = ++ nx; else id[i][j] = ++ ny; } } for( int i = 1; i <= n; i ++ ) { for( int j = 1; j <= m; j ++ ) { map[id[i][j]][id[i][j]] = 0; if( !mark[i][j] ) for( int d = 0; d < 4; d ++ ) { xx = i + xs[d]; yy = j + ys[d]; if( xx >= 1 && xx <= n && yy >= 1 && yy <= m && !mark[xx][yy] ) { if( (i+j)%2 == 0 ) map[id[i][j]][id[xx][yy]] = 1; else map[id[xx][yy]][id[i][j]] = 1; } } } } int ans = MaxMatch(); if( nx + ny - K == ans * 2 ) puts("YES"); else puts("NO"); } return 0; }
相关文章推荐
- 【POJ】2446 Chessboard 二分匹配
- POJ 2446 Chessboard (二分匹配)
- POJ 2446 Chessboard 二分匹配,藏得太深了
- poj 2446(二分匹配) Chessboard
- POJ 2446 Chessboard(二分匹配-hungary)
- poj 2446 Chessboard (最大二分匹配)
- Poj 2446 Chessboard 【二分匹配+经典建图模型】
- poj 2446 Chessboard (二分匹配)
- POJ 2446 Chessboard (二分匹配)
- poj 2446 Chessboard(经典二分匹配)
- POJ-2446 Chessboard 最大匹配
- poj Chessboard 2446 (最大匹配&转换)
- poj 2446 Chessboard (二分图利用奇偶性匹配)
- 【二分图+最大匹配】北大 poj 2446 Chessboard
- POJ 2446 Chessboard 匹配/最小路径覆盖
- POJ 2446 Chessboard(二分图最大匹配)
- POJ 2446 Chessboard(二分图最大匹配)
- poj 2446 Chessboard 二分图最大匹配经典题
- POJ 2446-Chessboard(二分图_最大匹配)
- poj 2446(二分匹配)