Showing posts with label Maximum Bipartite Matching 2D. Show all posts
Showing posts with label Maximum Bipartite Matching 2D. Show all posts

Sunday, January 6, 2019

[UVa] 11419 - SAM I AM

Author            : Dipu Kumar Mohanto 
                    CSE, Batch - 6
                    BRUR.
Problem Statement : 11419 - SAM I AM
Source            : UVa Online Judge
Category          : Graph Theory
Algorithm         : Maximum Bipartite Matching 2D, Kuhns Algorithm
Verdict           : Accepted
#include "bits/stdc++.h"
 
using namespace std;
 
static const int maxn = 5e3 + 5;
 
vector <int> bipertite_graph[maxn];  // row -> column
int row, column, k;
 
int visrow[maxn], viscolumn[maxn];
int Left[maxn], Right[maxn], seen[maxn];
 
inline bool kuhn(int r)
{
      visrow[r] = 1;
      for (int c : bipertite_graph[r])
      {
            if (viscolumn[c] == 0)
            {
                  viscolumn[c] = 1;
                  if (Right[c] == -1 || kuhn(Right[c]))
                  {
                        Right[c] = r;
                        Left[r] = c;
                        return 1;
                  }
            }
      }
      return false;
}
 
inline int bpm()
{
      for (int i = 0; i < maxn; i++)
      {
            Left[i] = Right[i] = -1;
            visrow[i] = viscolumn[i] = 0;
      }
      int match = 0;
      for (int i = 1; i <= row; i++)
      {
            for (int j = 0; j < maxn; j++) viscolumn[j] = 0;
            if (kuhn(i)) match++;
      }
      return match;
}
 
int main()
{
      //freopen("in.txt", "r", stdin);
 
      while (cin >> row >> column >> k)
      {
            if (row == 0 && column == 0 && k == 0) break;
            for (int i = 0; i < k; i++)
            {
                  int x, y;
                  cin >> x >> y;
                  y += row;
                  bipertite_graph[x].push_back(y);
            }
            int maximum_bipertite_matching = bpm();
            printf("%d", maximum_bipertite_matching);
 
            for (int i = 0; i < maxn; i++) visrow[i] = viscolumn[i] = 0;
            for (int i = 1; i <= row; i++) if (Left[i] == -1) kuhn(i);
 
            for (int i = 1; i <= row; i++)
            {
                  if (!visrow[i]) printf(" r%d", i);
            }
            for (int i = row+1; i <= row+column; i++)
            {
                  if (viscolumn[i]) printf(" c%d", i - row);
            }
            puts("");
 
            for (int i = 0; i < maxn; i++) bipertite_graph[i].clear();
      }
}

[UVa Live] 6811 - Irrigation Lines

Author            : Dipu Kumar Mohanto 
                    CSE, Batch - 6
                    BRUR.
Problem Statement : 6811 - Irrigation Lines
Source            : UVa Live Archive
Category          : Graph Theory
Algorithm         : Maximum Bipartite Matching 2D, Kuhns Algorithm
Verdict           : Accepted
  1. #include "bits/stdc++.h"  
  2.    
  3. using namespace std;  
  4.    
  5. static const int maxn = 500 + 5;  
  6.    
  7. vector <int> bipertite_graph[maxn];  
  8. int row, column;  
  9.    
  10. inline void addEdge(int u, int v)  
  11. {  
  12.       bipertite_graph[u].push_back(v);  
  13.       bipertite_graph[v].push_back(u);  
  14. }  
  15.    
  16. inline void clean()  
  17. {  
  18.       for (int i = 0; i < maxn; i++) bipertite_graph[i].clear();  
  19. }  
  20.    
  21. int Left[maxn], Right[maxn], seen[maxn];  
  22.    
  23. inline bool kuhn(int u)  
  24. {  
  25.       for (int v : bipertite_graph[u])  
  26.       {  
  27.             if (seen[v]) continue;  
  28.             seen[v] = 1;  
  29.             if (Right[v] == -1 || kuhn(Right[v]))  
  30.             {  
  31.                   Right[v] = u;  
  32.                   Left[u] = v;  
  33.                   return 1;  
  34.             }  
  35.       }  
  36.       return 0;  
  37. }  
  38.    
  39. inline int bpm()  
  40. {  
  41.       for (int i = 0; i < maxn; i++) Left[i] = Right[i] = -1;  
  42.       int match = 0;  
  43.       for (int i = 1; i <= row; i++)  
  44.       {  
  45.             for (int j = 0; j < maxn; j++) seen[j] = 0;  
  46.             if (kuhn(i)) match++;  
  47.       }  
  48.       return match;  
  49. }  
  50.    
  51. int main()  
  52. {  
  53.       //freopen("in.txt", "r", stdin);  
  54.    
  55.       int tc;  
  56.       cin >> tc;  
  57.       for (int tcase = 1; tcase <= tc; tcase++)  
  58.       {  
  59.             cin >> row >> column;  
  60.             string s;  
  61.             for (int i = 1; i <= row; i++)  
  62.             {  
  63.                   cin >> s;  
  64.                   for (int j = 1; j <= column; j++)  
  65.                   {  
  66.                         if (s[j-1] == '1') addEdge(i, j + row);  
  67.                   }  
  68.             }  
  69.             int maximum_bipertite_matching = bpm();  
  70.             cout << "Case #" << tcase << ": " << maximum_bipertite_matching << endl;  
  71.             clean();  
  72.       }  
  73. }  
  74.