Chessboard------最大独立数

Chessboard

Time Limit(Common/Java):1000MS/3000MS          Memory Limit:65536KByte
Total Submit:12            Accepted:12

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

HHUACM

 

#include<iostream> #include<vector> using namespace std; vector< vector<int> >adj; int a[33][33]={0},m,n,K,loc=0; int pre[500]={0},flag[1009]; void get_a(int i,int j) { int t,flag=0; if(a[i][j])return; if(i+1<=m&&a[i+1][j]==0) { if(flag==0) { flag++; loc++; } t=i*m+j; adj[loc].push_back(t); } if(i-1>=m&&a[i-1][j]==0) { if(flag==0) { flag++; loc++; } t=(i-2)*m+j; adj[loc].push_back(t); } if(j+1<=m&&a[i][j+1]==0) { if(flag==0) { flag++; loc++; } t=(i-1)*m+j+1; adj[loc].push_back(t); } if(j-1<=m&&a[i][j-1]==0) { if(flag==0) { flag++; loc++; } t=(i-1)*m+j-1; adj[loc].push_back(t); } } bool work(int i) { int j,t; for(j=0;j<adj[i].size();j++) { t=adj[i][j]; if(flag[t]==0) { if(pre[t]==0||work(pre[t])) { pre[t]=i; return true; } } } return false; } int hungary() { int sum=0,i; for(i=1;i<=loc;i++) { memset(flag,0,sizeof(flag)); if(work(i)) { sum++; } } return sum; } int main() { int i,j,p,q; cin>>m>>n>>K; adj.assign(m*n/2+2,vector<int>()); for(i=1;i<=K;i++) { cin>>q>>p; a[p][q]=1; } for(i=1;i<=m;i++) { for(j=1;j<=n;j++) { if((i+j)%2==0) { get_a(i,j); } } } if(loc==(m*n-K)/2&&hungary()==(m*n-K)/2) { cout<<"YES"<<endl; } else { cout<<"NO"<<endl; } return 0; }

你可能感兴趣的:(input,each,output,pair)