Poj 2446 chessboard (bipartite graph uses parity matching)

Source: Internet
Author: User

Chessboard
Time limit:2000 ms   Memory limit:65536 K
Total submissions:13176   Accepted:4118

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 shoshould be covered with exactly one card.
2. One Card shoshould 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.

# Include "stdio. H "# include" string. H "# include" queue "using namespace STD; # define n 35 # define m using int G [N] [N], n, m; int dir [4] [2] = {, 0,-1,-,}; int mark [m], link [m]; int judge (int x, int y) {If (x> = 0 & x <n & Y> = 0 & Y <m) return 1; return 0;} int find (int K) {int I, j, X, Y, Di, DJ; X = K/m; y = K % m; for (I = 0; I <4; I ++) {di = dir [I] [0] + X; dj = dir [I] [1] + Y; If (Judge (Di, DJ )&&! G [di] [DJ]) {J = di * m + DJ; If (! Mark [J]) {mark [J] = 1; if (link [J] =-1 | find (link [J]) {link [J] = K; return 1 ;}}} return 0 ;}int main () {int u, v, k, I, J; while (scanf ("% d", & N, & M, & K )! =-1) {memset (G, 0, sizeof (g); for (I = 0; I <K; I ++) {scanf ("% d", & V, & U); U --; V --; G [u] [v] = 1 ;} if (n * m-k) & 1) {printf ("NO \ n"); continue;} int ans = 0; memset (link,-1, sizeof (Link); for (I = 0; I <n; I ++) {for (j = 0; j <m; j ++) {If (I + J) % 2 = 0 | G [I] [J]) // (I + J) Parity !!! Continue; memset (mark, 0, sizeof (Mark); ans + = find (I * m + J) ;}// printf ("% d \ n ", ans); If (ANS * 2 = N * m-k) printf ("Yes \ n"); else printf ("NO \ n");} return 0 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.