Advertisement
Guest User

Untitled

a guest
Mar 25th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. #include <vector>
  2. #include <iostream>
  3. #include <iomanip>
  4.  
  5. using namespace std;
  6.  
  7. vector<vector<int>> p1 = { {0,4,2,2},{6,0,0,4},{0,0,6,4},{8,4,0,0}};
  8. vector<vector<int>> p2 = { {0,6,4,4},{4,0,0,4},{0,2,8,6},{6,4,0,0}};
  9.  
  10. vector<vector<int>> bipartee(const vector<vector<int>>& q)
  11. {
  12. vector<vector<int>> p;
  13. for(size_t r = 0; r < q.size(); ++r)
  14. {
  15. p.push_back(vector<int>());
  16. for(size_t c = 0; c < q.size(); ++c)
  17. if (q[r][c] == 0) p[r].push_back(c);
  18. }
  19. return p;
  20. }
  21.  
  22. bool try_kuhn(int v, vector<vector<int>>& g, vector<int>& mt, vector<bool>& used)
  23. {
  24. if (used[v]) return false;
  25. used[v] = true;
  26. for (size_t i=0; i<g[v].size(); ++i) {
  27. int to = g[v][i];
  28. if (mt[to] == -1 || try_kuhn(mt[to],g,mt,used))
  29. {
  30. mt[to] = v;
  31. return true;
  32. }
  33. }
  34. return false;
  35. }
  36.  
  37. bool check(const vector<vector<int>>& p)
  38. {
  39. int n = p.size();
  40. vector<vector<int>> g = bipartee(p);
  41. vector<int> mt;
  42. vector<bool> used;
  43. mt.assign(n, -1);
  44. for(int v = 0; v < n; ++v) {
  45. used.assign(n,false);
  46. try_kuhn(v,g,mt,used);
  47. }
  48.  
  49. int sum = 0;
  50. for (int i=0; i<n; ++i)
  51. if (mt[i] != -1) ++sum;
  52. return (sum == n);
  53. }
  54.  
  55.  
  56. int main()
  57. {
  58. cout << "First matrix: " << check(p1) << endl;
  59. cout << "Second matrix: " << check(p2) << endl;
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement