Cod sursa(job #2837512)

Utilizator cristi_macoveiMacovei Cristian cristi_macovei Data 22 ianuarie 2022 11:17:15
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.96 kb
#define d(x) std::cout << x << std::endl
#define dm(msg, x) std::cout << msg << x << std::endl
#define da(a, n) std::cout << "Array: ["; for (int _ = 1; _ <= n; ++_) {std::cout << a[_] << ' ';} std::cout << "]\n";
#define dma(m, a, n) std::cout << m << "["; for (int _ = 1; _ <= n; ++_) {std::cout << a[_] << ' ';} std::cout << "]\n";

#define all(a) a.begin(), a.end()
#define range(a, l, r) a.begin() + l, a.begin() + r
#define aall(a, n) a + 1, a + 1 + n
#define arange(a, l, r) a + l, a + r
	
#define maxself(a, b) a = std::max(a, b);
#define minself(a, b) a = std::min(a, b);

#define inout(f) std::ifstream in((f) + (std::string) ".in");std::ofstream out((f) + (std::string) ".out")
 
#include <iostream>
#include <fstream>
#include <vector>
#include <cstring>
 
const int NMAX = 1e4;

int n, m, e;

std::vector<int> graph[1 + NMAX]; // left to right

int matchLeft[1 + NMAX];
int matchRight[1 + NMAX];

bool vis[1 + NMAX];

bool match(int left) {
  if (vis[left])
    return false;

  vis[left] = true;

  for (int right : graph[left]) {
    if (matchLeft[right] == 0) {
      matchRight[left] = right;
      matchLeft[right] = left;
      return true;
    }
  }

  for (int right : graph[left]) {
    if (match(matchLeft[right])) {
      matchRight[left] = right;
      matchLeft[right] = left;
      return true;
    }
  }
  
  return false;
}

int main() {
  inout("cuplaj");
 
  in >> n >> m >> e;

  while (e--) {
    int a, b;
    in >> a >> b;

    graph[a].push_back(b);
  }
  
  bool ok = true;
  while (ok) {
    memset(vis, 0, sizeof(vis));
    ok = false;

    for (int i = 1; i <= n; ++i) {
      if (matchRight[i] == 0)
        ok |= match(i);
    }
  }

  int cnt = 0;
  for (int i = 1; i <= n; ++i) {
    if (matchRight[i])
      ++cnt;
  }

  out << cnt << '\n';

  for (int i = 1; i <= n; ++i) {
    if (matchRight[i])
      out << i << ' ' << matchRight[i] << '\n';
  }
 
  return 0;
}