HDU 1281 board game (2-point matching), hdu1281

Source: Internet
Author: User

HDU 1281 board game (2-point matching), hdu1281
Zookeeper board gamesTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission (s): 2649 Accepted Submission (s): 1546


Problem Description James and Gardon are playing a game: For an N * M board, place as many "cars" as possible in the grid as possible in chess ", this makes it easy for them not to attack each other, But Gardon restricts that only some grids can be placed. xiaoxi easily solves this problem (see) note that the locations where vehicles cannot be placed do not affect the mutual attack of vehicles.
So Gardon wants to solve a more difficult problem. While there are as many "cars" as possible, some grids in the board can be avoided. That is to say, if you do not place a car on these grids, you can also ensure that as many "cars" as possible are put down. However, if some grids are left empty, they cannot be placed as many "cars" as possible. Such grids are called important points. Gardon wants John to figure out how many important points are there. Can you solve this problem?

 
The Input contains multiple groups of data,
The first line contains three numbers: N, M, and K (1 <N, M <= 100 1 <K <= N * M), indicating the height and width of the Board, and the number of grids that can be placed with "cars. The next K lines describe the information of all grids: each row contains two numbers X and Y, indicating the position of the grid in the checker.
 
Output:
Board T have C important blanks for L chessmen.
 
Sample Input
3 3 41 21 32 12 23 3 41 21 32 13 2
 
Sample Output
Board 1 have 0 important blanks for 2 chessmen.Board 2 have 3 important blanks for 3 chessmen.
 
AuthorGardon
Source hangdian ACM training team training competition (VI)

Solution: based on the knowledge of graph theory, we can know that the minimum coverage point is equal to the maximum number of matches. We can regard x and y as two independent sets, and then create a bipartite graph matching to find the maximum number of matches. That is, the maximum matching book = the minimum coverage point can be placed in several vehicles. For important points, we can assume that this point is an important point. We can first remove this point and run a 2-point match. It depends on whether the result has an impact on the minimum coverage. This is an important point. Code attachment
# Include <stdio. h> # include <string. h ># include <algorithm> using namespace std; int n, m; int mat [105] [105]; int vis [105]; int pre [105]; int find (int x) // match {int I; for (I = 1; I <= m; I ++) {if (! Vis [I] & mat [x] [I]) {vis [I] = 1; if (pre [I] =-1 | find (pre [I]) {pre [I] = x; return 1 ;}} return 0 ;} int solve () {int ans = 0, I; memset (pre,-1, sizeof (pre); // remember to initialize every two-point match. For (I = 1; I <= n; I ++) {memset (vis, 0, sizeof (vis); if (find (I) ans ++ ;} return ans;} int main () {int I, k, x, y, j; int c = 0; while (scanf ("% d ", & n, & m, & k )! = EOF) {memset (mat, 0, sizeof (mat); memset (pre,-1, sizeof (pre); for (I = 1; I <= k; I ++) {scanf ("% d", & x, & y); mat [x] [y] = true;} int sum = solve (); // first run a 2-point match to find the minimum coverage point. Int res = 0; // important point for (I = 1; I <= n; I ++) for (j = 1; j <= m; j ++) {if (mat [I] [j]) {mat [I] [j] = 0; // remove this vertex first, if (solve ()! = Sum) // If the matching result is not equal to the minimum coverage point, it indicates that there is an impact on the matching result. The important point is res ++. // the important point is plus one. Mat [I] [j] = 1; // restore this vertex .}} Printf ("Board % d have % d important blanks for % d chessmen. \ n", ++ c, res, sum) ;}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.