Advertisement
Tarche

school-dance-tarche

May 31st, 2021
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.15 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. struct State {
  6.     vector<vector<int>> ady;
  7.     vector<int> match;
  8.     vector<bool> visited;
  9. };
  10.  
  11. int augPath(State& state, int k) {
  12.     if (state.visited[k])
  13.         return 0;
  14.     state.visited[k] = true;
  15.  
  16.     for (auto a : state.ady[k]) {
  17.         if (!state.match[a] || augPath(state, state.match[a])) {
  18.             state.match[a] = k;
  19.             return 1;
  20.         }
  21.     }
  22.  
  23.     return 0;
  24. }
  25.  
  26. int main() {
  27.     int n, m, k;
  28.     cin >> n >> m >> k;
  29.  
  30.     State state;
  31.     state.ady.resize(n + m + 1);
  32.     state.match.resize(n + m + 1);
  33.     state.visited.resize(n + m + 1);
  34.  
  35.     for (int i = 0; i < k; i++) {
  36.         int a, b;
  37.         cin >> a >> b;
  38.         b += n;
  39.         state.ady[a].push_back(b);
  40.         state.ady[b].push_back(a);
  41.     }
  42.  
  43.     int ans = 0;
  44.     for (int i = 1; i <= n; i++) {
  45.         for (int j = 0; j <= n; j++)
  46.             state.visited[j] = false;
  47.  
  48.         ans += augPath(state, i);
  49.     }
  50.  
  51.     cout << ans << '\n';
  52.     for (int i = n + 1; i <= n + m; i++) {
  53.         if (state.match[i])
  54.             cout << state.match[i] << ' ' << i - n << '\n';
  55.     }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement