Advertisement
Guest User

Untitled

a guest
Feb 26th, 2018
2,322
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.95 KB | None | 0 0
  1. struct BipartiteMatcher {
  2.   vector<vector<int>> G;
  3.   vector<int> L, R, Viz;
  4.  
  5.   BipartiteMatcher(int n, int m) :
  6.   G(n), L(n, -1), R(m, -1), Viz(n) {}
  7.  
  8.   void AddEdge(int a, int b) {
  9.     G[a].push_back(b);
  10.   }
  11.  
  12.   bool Match(int node) {
  13.     if (Viz[node])
  14.       return false;
  15.     Viz[node] = true;
  16.    
  17.     for (auto vec : G[node]) {
  18.       if (R[vec] == -1) {
  19.         L[node] = vec;
  20.         R[vec] = node;
  21.         return true;
  22.       }
  23.     }
  24.    
  25.     for (auto vec : G[node]) {
  26.       if (Match(R[vec])) {
  27.         L[node] = vec;
  28.         R[vec] = node;
  29.         return true;
  30.       }
  31.     }
  32.    
  33.     return false;
  34.   }
  35.  
  36.   int Solve() {
  37.     bool ok = true;
  38.     while (ok--) {
  39.       fill(Viz.begin(), Viz.end(), 0);
  40.       for (int i = 0; i < (int)L.size(); ++i)
  41.         if (L[i] == -1)
  42.           ok |= Match(i);
  43.     }
  44.    
  45.     int ret = 0;
  46.     for (int i = 0; i < L.size(); ++i)
  47.       ret += (L[i] != -1);
  48.     return ret;
  49.   }
  50. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement