Cod sursa(job #2214192)

Utilizator dropsdrop source drops Data 18 iunie 2018 15:06:12
Problema Cuplaj maxim in graf bipartit Scor 80
Compilator cpp Status done
Runda Arhiva educationala Marime 0.99 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);
  scanf("%d %d %d", &n, &m, &c);
  for (int i = 0; i < c; ++i) {
    scanf("%d %d", &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++;
    }
  }
  printf("%d\n", sol);
  for (int i = 1; i <= n; ++i) {
    if (l[i]) printf("%d %d\n", i, l[i]);
  }

  return 0;
}