Cod sursa(job #2675280)

Utilizator retrogradLucian Bicsi retrograd Data 21 noiembrie 2020 12:09:22
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.65 kb
#ifdef LOCAL
#include "debug.hpp"
#else
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define debug(...)
#endif 

#include <bits/stdc++.h>

using namespace std;


struct Matcher {
  int n;
  vector<vector<int>> graph;
  vector<int> vis, mate;

  Matcher(int n) : n(n), graph(n), vis(n), mate(n, -1) {}

  void AddEdge(int a, int b) {
    graph[a].push_back(b);
    graph[b].push_back(a);
  }

  int mateup(int a, int b) {
    assert(mate[a] == -1);
    int ret = mate[b];
    mate[a] = b; mate[b] = a;
    if (ret != -1) mate[ret] = -1;
    return ret;
  }

  bool dfs(int node) {
    if (vis[node]) return false;
    vis[node] = true;

    for (auto vec : graph[node])
      if (mate[vec] == -1)
        return mateup(node, vec), true;

    for (auto vec : graph[node]) {
      int nnode = mateup(node, vec);
      if (dfs(nnode))
        return true;
      mateup(nnode, vec);
    }
    return false;
  }

  int Solve() {
    int ans = 0, ch = 1;
    while (ch--) {
      fill(vis.begin(), vis.end(), false);
      for (int i = 0; i < n; ++i)
        if (mate[i] == -1 && dfs(i))
          ++ans, ch = 1;
    }
    return ans;
  }
};


int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  ifstream cin("cuplaj.in");
  ofstream cout("cuplaj.out");

  int n, m, k; cin >> n >> m >> k;
  Matcher M(n + m);
  for (int i = 0; i < k; ++i) {
    int a, b; cin >> a >> b;
    M.AddEdge(a - 1, b + n - 1);
  }

  cout << M.Solve() << endl;
  for (int i = 0; i < n; ++i) {
    if (M.mate[i] != -1) {
      cout << i + 1 << " " << M.mate[i] - n + 1 << '\n';
    }
  }

  return 0;
}