poj 2446 Chessboard (二分匹配)
2014-06-10 12:50
465 查看
Chessboard
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
Sample Output
Hint
A possible solution for the sample input.
Source
POJ Monthly,charlescpp
和 hdu 1507类似,构无向图然后判断匹配数是否等于合法的格数。
心算32*32错了= = RE了两次,开始以为32*32是90+,第二次以为是900+,笔算后才知道是1024..
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 12800 | Accepted: 4000 |
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
和 hdu 1507类似,构无向图然后判断匹配数是否等于合法的格数。
心算32*32错了= = RE了两次,开始以为32*32是90+,第二次以为是900+,笔算后才知道是1024..
//224K 125MS C++ 1731B 2014-06-10 12:44:41 #include<iostream> #include<vector> #define N 1050 using namespace std; vector<int>V ; int match ; int vis ; int g[35][35]; int dfs(int u) { for(int i=0;i<V[u].size();i++){ int v=V[u][i]; if(!vis[v]){ vis[v]=1; if(match[v]==-1 || dfs(match[v])){ match[v]=u; return 1; } } } return 0; } int hungary(int n) { int ret=0; memset(match,-1,sizeof(match)); for(int i=1;i<=n;i++){ memset(vis,0,sizeof(vis)); ret+=dfs(i); } return ret; } int main(void) { int n,m,k,x,y; while(scanf("%d%d%d",&n,&m,&k)!=EOF) { memset(g,0,sizeof(g)); for(int i=0;i<N;i++) V[i].clear(); for(int i=0;i<k;i++){ scanf("%d%d",&y,&x); g[x-1][y]=1; } int map ={0},pos=0; for(int i=0;i<n;i++) for(int j=1;j<=m;j++) if(!g[i][j]){ if(!map[i*m+j]) map[i*m+j]=++pos; int u=map[i*m+j]; if(j<m && !g[i][j+1]){ if(!map[i*m+j+1]) map[i*m+j+1]=++pos; V[u].push_back(map[i*m+j+1]); V[map[i*m+j+1]].push_back(u); } if(i<n-1 && !g[i+1][j]){ if(!map[(i+1)*m+j]) map[(i+1)*m+j]=++pos; V[u].push_back(map[(i+1)*m+j]); V[map[(i+1)*m+j]].push_back(u); } } //printf("%d\n",pos); if(hungary(pos)==pos) puts("YES"); else puts("NO"); } return 0; }
相关文章推荐
- 【POJ】2446 Chessboard 二分匹配
- 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 Chessboard 2446 (最大匹配&转换)
- POJ 2446 Chessboard(二分图最大匹配)
- POJ 2446 Chessboard(二分图最大匹配)
- POJ 2446-Chessboard(二分图_最大匹配)
- POJ 2446 Chessboard(二分图最大匹配)
- [POJ] 2446 Chessboard(二分图最大匹配)
- POJ 2446Chessboard(二分图最大匹配)
- 【二分图+最大匹配】北大 poj 2446 Chessboard
- POJ-2446 Chessboard 最大匹配