DuongNhi99

MATCH1

Dec 9th, 2020
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.98 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. const int N = 105;
  5.  
  6. int n, k;
  7. vector<int> g[N];
  8. int used[N];
  9. int match[N];
  10. int timer, ans;
  11.  
  12. bool try_kuhn(int u) {
  13.     if(used[u] == timer) return false;
  14.  
  15.     used[u] = timer;
  16.     for(int v : g[u]) {
  17.         if(match[v] == 0 || try_kuhn(match[v])) {
  18.             match[v] = u;
  19.             return true;
  20.         }
  21.     }
  22.     return false;
  23. }
  24.  
  25. int main() {
  26.     //freopen("in.txt", "r", stdin);
  27.     //freopen("MATCH1.inp", "r", stdin);
  28.     //freopen("MATCH1.out", "w", stdout);
  29.     ios_base::sync_with_stdio(false);
  30.     cin.tie(NULL); cout.tie(NULL);
  31.  
  32.     cin >> n >> k;
  33.  
  34.     int u, v;
  35.     while(cin >> u >> v) {
  36.         g[u].push_back(v);
  37.     }
  38.  
  39.     timer = ans = 0;
  40.     for(int i = 1; i <= n; ++i) {
  41.         timer++;
  42.         ans += try_kuhn(i);
  43.     }
  44.  
  45.     cout << ans << '\n';
  46.  
  47.     for(int i = 1; i <= k; ++i) {
  48.         if(match[i])
  49.             cout << match[i] << ' ' << i << '\n';
  50.     }
  51.  
  52.     return 0;
  53. }
  54.  
Advertisement
Add Comment
Please, Sign In to add comment