Board games (two-part graph matching)

Source: Internet
Author: User
Tags dashed line

Topic Connection: http://acm.hdu.edu.cn/showproblem.php?pid=1281Board games

Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 3200 Accepted Submission (s): 1897


Problem description and Gardon play a game: to a n*m chess board, in the lattice to put as much as possible some chess inside the "car", and so they can not attack each other, which is of course very simple, but Gardon limited only some lattice can be put, Xiaoxi is still easy to solve the problem (see) Note that the location of the car does not affect the car's mutual attack.
So now Gardon want to let Xiao-Xi to solve a more difficult problem, in order to ensure as much as possible "car" premise, some of the board can be avoided, that is, not on these squares to put the car, but also to ensure that as much as possible "car" was put down. However, if some lattice does not put the son, there is no guarantee to put as much as possible "car", such a lattice is called to do important points. Gardon want to figure out how many of these important points, can you solve this problem?

Input inputs contain multiple sets of data,
The first line has three numbers N, M, K (1<n,m<=100 1<k<=n*m), indicating the height and width of the board, and the number of squares that can be put "cars". The next K-line describes all the lattice information: two x and y per line, indicating the position of the lattice in the checkerboard.

Output for each set of data entered, as shown in the following format:
Board T has C important blanks for L chessmen.

Sample INPUT3 3 41 21 32 12 23 3 41 21 32 13 2

Sample Outputboard 1 has 0 important blanks for 2 chessmen. Board 2 has 3 important blanks for 3 chessmen.

Authorgardon

Source Hangzhou Electric ACM Training Team Training Competition (VI) Solution: This problem has a special method of mapping, and then can be cleverly converted to the problem of binary map matching, and then use the Hungarian algorithm to solve problems: No line as a left point, not a column as a point on the right, If this point can put a pawn, then the horizontal ordinate between the two-way edge, and then look for the largest two-figure matching (equivalent to each row and vertical line has only one focus, and make the total number of focal points) but this question to pay attention to the number of important point, And if the point will be removed to affect the total number of points in the final focus, so, every time a side to do one side of the two-sided graph matching, see Finally, if the results have an impact, then the number of important points plus a Hungarian algorithm: binary map matching the Hungarian algorithm: conditions, can be divided into two groups, two groups, There is no edge between the same group, one-to-one matching number of how many, if for any tree, you can use black-and-white staining method to divide the graph node into two DFS algorithm idea: For any point on the left, find an augmented road, notice is to find an augmented road stop looking (the number of edges is odd, And it is a dashed line, the actual situation to the end of the polyline, with a dashed line-the solid lines are already matched segments, dashed lines indicate unmatched) and then the augmented path of the solid line and dashed, so that the matching number plus 1, and then traverse the left all points, Then get all the number of the augmentation path is the final result (because each find an augmented path can make the total number of matches plus 1) can also be understood in a different way: a left person B to the right to find a friend, if he found a person who did not match, then directly with her friends, and if this person C has a friend A, So from this friend a start in the same way to see if he can find other friends, if possible, let a go to find other people to make friends, so B can be a friend C (so think, if a in addition to find D is still matched, then see D can find an unmatched it? If D found the E is still matched to see if e can and other people do friends, so keep looking, know to find a can change a person to do friends, and then replace, so the beginning of the now consider how to remove an edge, you can add a bool parameter in the structure of the edge is not still in the edge , see the Code
1#include <cstdio>2#include <cstring>3 using namespacestd;4 #defineN 2105 structedge{6     intto ;7     BOOL inch;8     intNext;9}edge[n*N];Ten intHead[n]; One intenct; A BOOLvisited[n*N]; - intLink[n]; - voidInit () the { -memset (head,-1,sizeof(head)); -Enct =0; - } + voidAddint  from,intto ) - { +Edge[enct].to =to ; AEdge[enct].next = head[ from]; atEDGE[ENCT].inch=true; -head[ from] = enct++; -Edge[enct].to = from; -EDGE[ENCT].inch=true; -Edge[enct].next =Head[to]; -Head[to] = enct++; in } - BOOLFindintu) to { +      for(inti = Head[u]; i!=-1; i =edge[i].next) -     { the         if(Edge[i].inch==false)Continue; *         intv =edge[i].to; $         if(Visited[v])Continue;Panax NotoginsengVISITED[V] =true; -         if(Link[v] = =-1||find (Link[v])) the         { +LINK[V] = u;//backtracking, reversing the edges each time A             //printf ("%d", Link[v]); the             return true; +         } -     } $     return false; $ } - intK; - intN; the intSolve () - {Wuyimemset (link,-1,sizeof(link)); the     intres =0; -      for(inti =1; I <= n; i++)//Note that the number of points is starting from 1. Wu     { -Memset (visited,0,sizeof(visited)); AboutVisited[i] =true; $         if(Find (i)) {res++;  -              //printf ("%d", Link[i]); -              //puts (""); -         } A     } +     returnRes; the } - intMain () $ { the     intm; the     intc =0; the      while(~SCANF ("%d%d%d",&n,&m,&k)) the     { -C++; in init (); the          for(inti =0; I < K; i++) the         { About             intx, y; thescanf"%d%d",&x,&y); theAdd (x,y+n); the         } +         intsum =solve (); -         intAns =0 ; the          for(inti =0; i < K; i++)Bayi         { theedge[i<<1|1].inch=false; theedge[i<<1].inch=false; -             intTM =solve (); -             if(tm!=sum) ans++; theedge[i<<1|1].inch=true; theedge[i<<1].inch=true; the         } theprintf"Board%d has%d important blanks for%d chessmen.\n", c,ans,sum); -     } the     return 0; the}

Board games (two-part graph matching)

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.