首页 > 代码库 > POJ2446_Chessboard(二分图最大匹配)
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\n"); 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\n"); 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\n"); else printf("NO\n"); } 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.
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
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。