Raslboyy

Алгоритм Куна

Nov 27th, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.77 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <list>
  4. #include <algorithm>
  5. #include <string>
  6. #include <math.h>
  7. #include <climits>
  8.  
  9. //#include <chrono>
  10. //#include <fstream>
  11. //#include <bits/stdc++.h>
  12.  
  13. #define mp make_pair
  14. #define pb push_back
  15. #define eb emplace_back
  16. #define x first
  17. #define y second
  18. #define sz(x) (int)x.size()
  19. #define all(x) begin(x), end(x)
  20. #define rall(x) rbegin(x), rend(x)
  21. #define FOR(i,a,b) for (int i = (a); i < (b); i++)
  22. #define RFOR(i,b,a) for (int i = (b) - 1; i >= (a); i--)
  23.  
  24. using namespace std;
  25.  
  26. typedef unsigned long long ull;
  27. typedef long long ll;
  28. typedef long double ld;
  29. typedef pair<int, int> pi;
  30. typedef pair<ll, ll> pl;
  31. typedef vector<int> veci;
  32. typedef vector<bool> vecb;
  33. typedef vector<vector<int>> vvi;
  34. typedef vector<vector<bool>> vvb;
  35.  
  36. const int INF_I = 1e9;
  37. const ll INF_LL = 1e18;
  38.  
  39. vvb vec;
  40. veci p;
  41. vecb used;
  42. int n, s;
  43.  
  44. bool dfs(int v) {
  45. if (used[v])
  46. return false;
  47. used[v] = true;
  48. for (int i = 0; i < n; i++)
  49. if (vec[v][i] && (p[i] == -1 || dfs(p[i]))) {
  50. p[i] = v;
  51. return true;
  52. }
  53. return false;
  54. }
  55.  
  56. int main() {
  57. /*ifstream cin("in.txt");
  58. ofstream cout("out.txt");
  59. auto start_time = chrono::high_resolution_clock::now();*/
  60.  
  61. ios::sync_with_stdio(false);
  62. cin.tie(0);
  63. cout.tie(0);
  64.  
  65.  
  66.  
  67. cin >> n >> s;
  68. vec.assign(s, vecb(n));
  69. p.assign(n, -1);
  70. for (int i = 0; i < s; i++)
  71. for (int j = 0; j < n; j++)
  72. {
  73. int a; cin >> a;
  74. vec[i][j] = a == 1;
  75. }
  76.  
  77.  
  78. int res = 0;
  79. for (int i = 0; i < s; i++) {
  80. used.assign(s, 0);
  81. res += dfs(i);
  82. }
  83.  
  84. cout << res << endl;
  85.  
  86. /*auto end_time = chrono::high_resolution_clock::now();
  87. chrono::duration<double> duration = end_time - start_time;
  88. cout << duration.count() << "\n";
  89. cin.close();
  90. cout.close();*/
  91. return 0;
  92. }
Add Comment
Please, Sign In to add comment