Advertisement
tien_noob

Maximum Matching Pairs

Nov 21st, 2021
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.38 KB | None | 0 0
  1. //Make CSP great again
  2. #include <bits/stdc++.h>
  3. #define TASK "TESTCODE"
  4. using namespace std;
  5. const int N = 5e2;
  6. vector<int> Adj[N + 1];
  7. int n, m, k, Assigned[N + 1], Visited[N + 1], t;
  8. void read()
  9. {
  10.     cin >> n >> m >> k;
  11.     for (int i = 1; i <= k; ++ i)
  12.     {
  13.         int u, v;
  14.         cin >> u >> v;
  15.         Adj[u].push_back(v);
  16.     }
  17. }
  18. bool DFS(int u)
  19. {
  20.     if (Visited[u] != t)
  21.     {
  22.         Visited[u] = t;
  23.     }
  24.     else
  25.     {
  26.         return false;
  27.     }
  28.     for (int v : Adj[u])
  29.     {
  30.         if (!Assigned[v] || DFS(Assigned[v]))
  31.         {
  32.             Assigned[v] = u;
  33.             return true;
  34.         }
  35.     }
  36.     return false;
  37. }
  38. void solve()
  39. {
  40.     int cnt = 0;
  41.     for (int i = 1; i <= n; ++ i)
  42.     {
  43.         ++t;
  44.         cnt += DFS(i);
  45.     }
  46.     cout << cnt << '\n';
  47.     for (int i = 1; i <= m; ++ i)
  48.     {
  49.         if (Assigned[i])
  50.         {
  51.             cout << Assigned[i] << ' ' << i << '\n';
  52.         }
  53.     }
  54. }
  55. int main()
  56. {
  57.     ios_base::sync_with_stdio(false);
  58.     cin.tie(nullptr);
  59.     if (fopen(TASK".INP", "r"))
  60.     {
  61.         freopen(TASK".INP", "r", stdin);
  62.         //freopen(TASK".OUT", "w", stdout);
  63.     }
  64.     int t = 1;
  65.     bool typetest = false;
  66.     if (typetest)
  67.     {
  68.         cin >> t;
  69.     }
  70.     for (int __ = 1; __ <= t; ++ __)
  71.     {
  72.         //cout << "Case " << __ << ": ";
  73.         read();
  74.         solve();
  75.     }
  76. }
  77.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement