POJ2446_Chessboard(二分图最大匹配)
来源:互联网 发布:洪玄公捏脸数据 编辑:程序博客网 时间:2024/06/03 16:34
解题报告
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: 65536KTotal 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 22 13 3
Sample Output
YES
Hint
A possible solution for the sample input.
Source
POJ Monthly,charlescpp
1 0
- POJ2446_Chessboard(二分图最大匹配)
- 二分图最大匹配
- 二分图最大匹配 。
- 二分图最大匹配
- 二分图最大匹配
- 二分图最大匹配
- 二分图最大匹配
- 二分图 最大匹配
- 二分图最大匹配
- 二分图最大匹配
- 二分图最大匹配
- 二分图最大匹配
- 二分图最大匹配
- 二分图最大匹配
- 二分图最大匹配
- 二分图最大匹配
- 二分图最大匹配
- 二分图 最大匹配
- Codeforces Round #240 (Div. 1)B
- 解决mysql忘记密码
- maven3实战之仓库(镜像)
- Codeforces 237C
- Stanford Machine Learning 公开课笔记(5) Machine Learning System Design
- POJ2446_Chessboard(二分图最大匹配)
- Hdu1879 - 继续畅通工程 - 最小生成树
- 分析进程和线程的区别
- 使用libzplay库封装一个音频类
- VC多线程C运行时库 /ML /MLd /MT /MTd /MD /MDd
- 菜鸟学习javaweb(一)--自动拆装箱
- href="javascript:void(0);"与#的区别
- http://blog.csdn.net/wangjian8006/article/details/7623199
- Linux文件误删除恢复操作