rembocoder

Untitled

Apr 15th, 2023
709
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.  
  3. using namespace std;
  4.  
  5. #define int int64_t
  6.  
  7. const int inf = 2e18;
  8. const int mod = 1e9 + 7;
  9.  
  10. vector<vector<int>> g;
  11. vector<int> p;
  12. vector<bool> used;
  13.  
  14. bool dfs(int v) {
  15.     used[v] = true;
  16.     for (int to: g[v]) {
  17.         if (p[to] == -1 || !used[p[to]] && dfs(p[to])) {
  18.             p[to] = v;
  19.             return true;
  20.         }
  21.     }
  22.     return false;
  23. }
  24.  
  25. int32_t main() {
  26.     ios_base::sync_with_stdio(0);
  27.     cin.tie(0); cout.tie(0);
  28.     int n1, n2, m;
  29.     cin >> n1 >> n2 >> m;
  30.     g.resize(n1);
  31.     for (int i = 0; i < m; i++) {
  32.         int a, b;
  33.         cin >> a >> b;
  34.         a--;
  35.         b--;
  36.         g[a].push_back(b);
  37.     }
  38.     p.assign(n2, -1);
  39.     int ans = 0;
  40.     for (int i = 0; i < n1; i++) {
  41.         used.assign(n1, false);
  42.         ans += dfs(i);
  43.     }
  44.     cout << ans << '\n';
  45.     for (int i = 0; i < n2; i++) {
  46.         if (p[i] != -1) {
  47.             cout << p[i] + 1 << ' ' << i + 1 << '\n';
  48.         }
  49.     }
  50. }
  51.  
Advertisement
Add Comment
Please, Sign In to add comment