Advertisement
dipBRUR

MATCHING - Fast Maximum Matching

Aug 8th, 2017
519
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.81 KB | None | 0 0
  1. /**
  2.  * Author                      : Dipu Kumar Mohanto (Dip)
  3.  *                               CSE, BRUR.
  4.  *                               Batch - 6
  5.  *
  6.  * Problem                     : MATCHING - Fast Maximum Matching
  7.  * Algorithm                   : Maximum Matching (Hopcroft)
  8.  * Complexity                  : O(sqrt(V) * E)
  9.  * Category                    : Graph Theory, Maximum Bipartite Matching
  10.  *
  11.  *  
  12.  * Difficulty                  : Meddium
  13.  *
  14.  * Source                      : SPOJ Online Judge
  15.  *
  16.  * Verdict                     : Accepted
  17.  *
  18.  * Date                        :
  19.  * E-mail                      : dipukumarmohanto1@gmail.com
  20. **/
  21.  
  22. // solution - 2
  23. #include <bits/stdc++.h>
  24.  
  25. using namespace std;
  26.  
  27. #define MAX 150007
  28. #define NIL 0
  29. #define INF (1<<28)
  30.  
  31. vector< int > G[MAX];
  32. int n, m, match[MAX], dist[MAX];
  33. // n: number of nodes on left side, nodes are numbered 1 to n
  34. // m: number of nodes on right side, nodes are numbered n+1 to n+m
  35. // G = NIL[0]  G1[G[1---n]] G2[G[n+1---n+m]]
  36.  
  37. bool bfs() {
  38.     int i, u, v, len;
  39.  
  40.     queue< int > Q;
  41.  
  42.     for(i=1; i<=n; i++)
  43.     {
  44.         if(match[i]==NIL)
  45.         {
  46.             dist[i] = 0;
  47.             Q.push(i);
  48.         }
  49.         else
  50.             dist[i] = INF;
  51.     }
  52.     dist[NIL] = INF;
  53.    
  54.    
  55.  
  56.  
  57.  
  58.  
  59.     while(!Q.empty())
  60.     {
  61.         u = Q.front(); Q.pop();
  62.    
  63.         if(u!=NIL)
  64.         {
  65.             len = G[u].size();
  66.             for(i=0; i<len; i++)
  67.             {
  68.                 v = G[u][i];
  69.                 if(dist[match[v]]==INF)
  70.                 {
  71.                     dist[match[v]] = dist[u] + 1;
  72.                     Q.push(match[v]);
  73.                 }
  74.             }
  75.         }
  76.     }
  77.     return (dist[NIL]!=INF);
  78. }
  79.  
  80. bool dfs(int u)
  81. {
  82.     int i, v, len;
  83.    
  84.     if(u!=NIL)
  85.     {
  86.         len = G[u].size();
  87.         for(i=0; i<len; i++)
  88.         {
  89.             v = G[u][i];
  90.             if(dist[match[v]]==dist[u]+1)
  91.             {
  92.                 if(dfs(match[v]))
  93.                 {
  94.                     match[v] = u;
  95.                     match[u] = v;
  96.                     return true;
  97.                 }
  98.             }
  99.         }
  100.         dist[u] = INF;
  101.         return false;
  102.     }
  103.     return true;
  104. }
  105.  
  106. int hopcroft_karp()
  107. {
  108.     int matching = 0, i;
  109.     // match[] is assumed NIL for all vertex in G
  110.     while(bfs())
  111.         for(i=1; i<=n; i++)
  112.             if(match[i]==NIL && dfs(i))
  113.                 matching++;
  114.     return matching;
  115. }
  116.  
  117. int main()
  118. {
  119.     int pairs ;
  120.     scanf("%d %d %d",&n,&m,&pairs);
  121.  
  122.     while( pairs-- ){
  123.         int x , y ;
  124.         scanf("%d %d",&x,&y);
  125.  
  126.         G[x].push_back(y+n);
  127.         G[y+n].push_back(x);
  128.     }
  129.     printf("%d\n",hopcroft_karp());
  130. }
  131.  
  132. /************  Input Output    ***************/
  133. 5 4 6
  134. 5 2
  135. 1 2
  136. 4 3
  137. 3 1
  138. 2 2
  139. 4 4
  140.  
  141. 3
  142. /********************************************/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement