Part 1 What is a binary diagram
The two-part graph, also known as two-part graph, is a special model in graph theory. Set g= (V,e) is a graph, if vertex V can be divided into two disjoint subsets (A, b), and each edge (I,J) in the diagram is associated with two vertices I and J respectively belong to these two different vertex sets (I in A,j in B), it is said that figure G is a two-part graph.
This graph is a two-part diagram
Part 2 What is a binary graph match
Given a binary graph G, in a sub-figure m of G, any two edges in M's edge set {E} are not attached to the same vertex, then M is a match.
The maximum match (maximal Matching) is the number of matched edges that can no longer be increased by increasing the unmatched matching edge, under the currently completed match. The maximum match (maximum matching) is a match of the largest number of sides in any maximal match. Selecting a subset of such a number of sides is called the maximum matching problem for graphs. if one match, each vertex in the diagram is associated with an edge in the graph, it is said to be an exact match, also known as a complete match. to find the maximum match of binary graph can be used maximum flow (maximal flow) or Hungarian algorithm (Hungarian algorithm)Part
3 Hungarian algorithmThe main operation of the Hungarian algorithm algorithm is to enumerate the left point, find its first edge with the right corner, and then if the connected point is already matched with other points, match the previous point to the other points that it is connected to, and repeat the operation again, if the previous point cannot match the other point, the current enumerated point enumerates its next edge If all edges are not working, this point does not participate in matching
Code#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <vector>
using namespace std;
int g[1100][1100];
int used[1100],wh[1100];
int ans;
int t,n,m;
inline BOOL Work (int. now) {
int i,j,k;
For (j=1;j<=m;j++)
if (Used[j]!=t&&g[now][j]) {
used[j]=t;
if (!wh[j]| | Work (Wh[j])) {
Wh[j]=now;
return 1;
}
}
return 0;
}
inline void Go () {
int i,j,k;
For (i=1;i<=n;i++) {
t=i;
if (work (i)) ans++;
}
}
int main () {
int i,j,k,x,y;
cin>>n>>m>>k;
For (i=1;i<=k;i++) {
cin>>x>>y;
if (y<=m)
g[x][y]=1;
}
go ();
cout<<ans<<endl;
return 0;
}
Bonus
Max Stream procedure Code#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdlib>
#include <queue>
#include <ctime>
#include <vector>
#include <set>
#include <map>
#include <stack>
using namespace std;
const int inf=1e9+7;
struct edge{
int c,to,next;
}e[2000000];
int head[5000],level[5000],cnt;
void Read (int &x) {
int f=1;x=0;
Char S=getchar ();
While (s< ' 0 ' | | S> ' 9 ') {if (s== '-') F=-1;s=getchar ();}
while (s>= ' 0 ' &&s<= ' 9 ') {x=x*10+ (S-' 0 '); S=getchar ();}
x= (f==1?x:-x);
}
void Add (int u,int v,int W) {
e[cnt].c=w;
E[cnt].to=v;
E[cnt].next=head[u];
head[u]=cnt++;
E[cnt].to=u;
E[cnt].next=head[v];
head[v]=cnt++;
}
int BFS (int s,int t) {
queue<int>q;
memset (level) (level,-1,sizeof);
Q.push (s);
level[s]=0;
While (!q.empty ()) {
int u,v;
U=q.front ();
Q.pop ();
for (int i=head[u];~i;i=e[i].next) {
v=e[i].to;
if (level[v]==-1&&e[i].c) {
level[v]=level[u]+1;
Q.push (v);
if (v==t) return 1;
}
}
}
if (level[t]==-1) return 0;
return 1;
}
int dfs (int u,int v,int flow) {
if (u==v) return flow;
int res=0;
for (int i=head[u];~i;i=e[i].next) {
int j=e[i].to;
if (level[j]==level[u]+1&&e[i].c) {
int F=dfs (j,v,min (e[i].c,flow-res));
res+=f;
e[i].c-=f;
e[i^1].c+=f;
}
}
if (!res) level[u]=-1;
return res;
}
int main ()
{int n,m,e,i,j,k,u,v,ans=0;
read (n);
read (m);
read (E);
memset (head,-1,sizeof (head));
For (i=1;i<=e;i++) {
read (u);
Read (v);
if (v>m) continue;
Add (u,v+n,1);
}
int s=n+m+1,t=n+m+2;
For (i=1;i<=n;i++) {
Add (s,i,1);
}
For (i=1;i<=m;i++) {
Add (n+i,t,1);
}
While (BFS (s,t))
Ans+=dfs (s,t,inf);
printf ("%d\n", ans);
return 0;
}
The Hungarian algorithm of binary graph matching