Cod sursa(job #2214190)

Utilizator dropsdrop source drops Data 18 iunie 2018 15:04:56
Problema Cuplaj maxim in graf bipartit Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 0.97 kb
#include <iostream>
#include <unordered_map>
#include <unordered_set>
#include <set>
#include <queue>
#include <memory>
#include <algorithm>
#include <map>
#include <stack>
#include <vector>
#include <string>
#include <cstring>
#include <functional>
using namespace std;

bool was[10100];
vector<int> g[10100];
int l[10100], r[10100];

bool dfs(int x) {
  if (was[x]) return false;
  was[x] = true;
  for (auto y : g[x]) {
    if (r[y] == 0 || dfs(r[y])) {
      l[x] = y;
      r[y] = x;
      return true;
    }
  }
  return false;
}

int main() {
  int n, m, c, x, y, sol = 0;
  freopen("cuplaj.in","r",stdin);
  freopen("cuplaj.out","w",stdout);
  cin >> n >> m >> c;
  for (int i = 0; i < c; ++i) {
    cin >> x >> y;
    g[x].push_back(y);
  }
  for (int i = 1; i <= n; ++i) {
    if (l[i] == 0 && dfs(i)) {
      memset(was, 0, sizeof(was));
      sol++;
    }
  }
  cout << sol << endl;
  for (int i = 1; i <= n; ++i) {
    if (l[i]) cout << i << " " << l[i] << endl;
  }

  return 0;
}