Topic links: http://acm.hdu.edu.cn/showproblem.php?pid=2063
Hungary augmenting path algorithm is based on a recursive algorithm, applied to the bipartite graph matching problem. This problem is a Hungarian algorithm for maximum matching of bipartite graph application. For V1 a to be matched in the point x, can be considered to the point of him a potential V2 y in a matching object, if this point there is no match over or give y found a new matching objects can be y as an official of x match.
This question I use is the adjacency matrix, each search augmenting path time is O (| V | ^ 2), so the total time complexity is O (| V | ^ 3), the total space complexity is O (| V | ^ 2), if implemented, then the adjacency list total time complexity is O (| V || E |), the spatial complexity is O (| E | + | V |).
code show as below:
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef unsigned int ui; 4 typedef long long ll; 5 typedef unsigned long long ull; 6 #define pf printf 7 #define mem(a,b) memset(a,b,sizeof(a)) 8 #define prime1 1e9+7 9 #define prime2 1e9+9 10 #define pi 3.14159265 11 #define lson l,mid,rt<<1 12 #define rson mid+1,r,rt<<1|1 13 #define scand(x) scanf("%llf",&x) 14 #define f(i,a,b) for(int i=a;i<=b;i++) 15 #define scan(a) scanf("%d",&a) 16 #define mp(a,b) make_pair((a),(b)) 17 #define P pair<int,int> 18 #define dbg(args) cout<<#args<<":"<<args<<endl; 19 #define inf 0x7ffffff 20 inline int read(){ 21 int ans=0,w=1; 22 char ch=getchar(); 23 while(!isdigit(ch)){if(ch=='-')w=-1;ch=getchar();} 24 while(isdigit(ch))ans=(ans<<3)+(ans<<1)+ch-'0',ch=getchar(); 25 return ans*w; 26 } 27 int k,n,m; 28 const int maxn=1e3; 29 int g[maxn][maxn]; 30 int m_girl,n_boy; 31 int match[maxn],reserve_boy[maxn];//匹配结果在match中 32 boolDFS ( int x) // Hungarian algorithm: for x to find an augmenting path, i.e. to find a matching object 33 is { 34 is for ( int I = . 1 ; I <= n_boy; I ++ ) 35 { 36 IF (reserve_boy [I! ] && G [x] [i]) // boy i and girls are not preserve object x may be matched and i 37 [ { 38 is reserve_boy [i] = . 1 ; // predetermined boy i, ready to give x 39 IF (match! [i] || DFS (match [i])) 40 // there are two cases, (1) the boy i no matching object (2) boys paired objects have now attempt to replace the boy i Original objects both a successful i assigned to put X 41 is { 42 is match [i] = X; // i-th pair boy or directly to the object to replace X 43 is return to true ; 44 is } 45 } 46 is } 47 return to false ; // girls i pairing fails 48 } 49 int main () 50 { 51 is // The freopen ( "input.txt", "R & lt", stdin); 52 is // The freopen ( "output.txt", "W", stdout); 53 is STD :: :: iOS sync_with_stdio ( to false ); 54 is the while (Scanf ( " % D " , & K) ==1&&k) 55 { 56 m_girl=read(),n_boy=read(); 57 mem(g,0); 58 mem(match,0); 59 int u,v; 60 f(i,1,k) 61 { 62 u=read(),v=read(); 63 g[u][v]=1;//邻接矩阵存边 64 } 65 int sum=0; 66 f(i,1,m_girl)//Looking for each girl pair, the current seek successful girl after may be replaced, but will successfully paired 67 { 68 MEM (reserve_boy, 0 ); 69 IF (DFS (I)) SUM ++ ; 70 } 71 is PF ( " % D \ n- " , SUM); 72 } 73 is }