Test instructions: To a n*m chess board, on the top of the car, put the cars can not attack each other (in the same row or the same column will be able to attack each other), and only some points can be put in the car. Ask how many cars you can put, how many of them must be put in order to put the most cars.
This is a good understanding of the Hungarian algorithm of the topic. First we ask for the maximum number of cars, this is a row and column matching problem. Suppose we use the n left point to represent the row, the M-right point on the right, and if a lattice (x, y) is able to put the car, then the X on the left and the Y connection on the right side are built together. This is a very classic model, it does not need much. The Hungarian algorithm was used to find the maximum match.
The following question is very good. First of all, I'm talking about the most violent method. Record the matching edge in the maximum match, and then remove the edge from the original image to see if the maximum match is equal to the original one. Once the complexity of Hungary was O (n*m^2).
A total of n Hungary, the overall complexity is O (n^2*m^2), for this problem 100 of the data is able to AC. But if the amount of data increases, it will be a different method. In fact, after finding the maximum match, suppose (X->y) is an edge in the maximum match, we first delete the edge and make y excuse. At this point the X is looking for a grace path, if it can be found, it means that we replace the match with another match (X->y), so (x->y) is not necessary. No, (x->y) is necessary. Note: if (x->y) is required, then to link[y]=x Yes X again matches y to reach the maximum match. In addition: no matter (x->) is not necessary, must restore the diagram.
Method One violence:
VIEW CODE
#include <cstdio> #include <algorithm> #include <iostream> #include <cmath> #include <queue > #include <stack> #include <string> #include <cstring> #include <map> #include <vector > #include <set> #include <ctime> #include <stdlib.h>using namespace std;const int mod=99999997; Const double Eps=1e-8;const double Pi=acos ( -1.0); const int Inf=0x3fffffff;bool g[110][110];int Link[110];int n,m;bool Vis[110];bool match (int x) {for (int i=1;i<=m;i++) {if (g[x][i]&& (!vis[i])) {Vis [I]=1; if (link[i]==-1| | Match (Link[i])) {link[i]=x; return 1; }}} return 0;} int Hungury () {int ans=0; memset (link,-1,sizeof link); for (int i=1;i<=n;i++) {memset (vis,0,sizeof vis); if (Match (i)) ans++; } return ans; int Link__[110];int Main () {int k,ca=0; while (cin>>n>>m>>k) { memset (g,0,sizeof G); while (k--) {int x, y; scanf ("%d%d", &x,&y); G[x][y]=1; } int Aa=hungury (); int ans=0; for (int i=1;i<=m;i++) link__[i]=link[i]; for (int i=1;i<=m;i++) {if (link__[i]+1) {int x=link__[i]; g[x][i]=0; if (Hungury ()!=aa) ans++; G[x][i]=1; }} printf ("Board%d has%d important blanks for%d chessmen.\n", ++CA,ANS,AA); } return 0;}
Method 2: Find the augmented road once
VIEW CODE
#include <cstdio> #include <algorithm> #include <iostream> #include <cmath> #include <queue > #include <stack> #include <string> #include <cstring> #include <map> #include <vector > #include <set> #include <ctime> #include <stdlib.h>using namespace std;const int mod=99999997; Const double Eps=1e-8;const double Pi=acos ( -1.0); const int Inf=0x3fffffff;bool g[110][110];int Link[110];int n,m;bool Vis[110];bool match (int x) {for (int i=1;i<=m;i++) {if (g[x][i]&& (!vis[i])) {Vis [I]=1; if (link[i]==-1| | Match (Link[i])) {link[i]=x; return 1; }}} return 0;} int Hungury () {int ans=0; memset (link,-1,sizeof link); for (int i=1;i<=n;i++) {memset (vis,0,sizeof vis); if (Match (i)) ans++; } return ans; int main () {int k,ca=0; while (cin>>n>>m>>k) {memset (g,0,sizEOF G); while (k--) {int x, y; scanf ("%d%d", &x,&y); G[x][y]=1; } int Aa=hungury (); int ans=0; for (int i=1;i<=m;i++) {if (link[i]+1) {int x=link[i]; g[x][i]=0; Link[i]=-1; memset (vis,0,sizeof Vis); if (!match (x)) {ans++; Link[i]=x; } g[x][i]=1; }} printf ("Board%d has%d important blanks for%d chessmen.\n", ++CA,ANS,AA); } return 0;}
< graph theory algorithm 2 matching > HDU 1281 (board game)