Cod sursa(job #2973374)

Utilizator cadmium_Voicu Mihai-Valeriu cadmium_ Data 31 ianuarie 2023 20:45:57
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.63 kb
#include <bits/stdc++.h>
#define all(x) (x).begin(),(x).end()
using namespace std;

#ifndef DLOCAL
  #define cin fin
  #define cout fout
  ifstream cin("cuplaj.in");
  ofstream cout("cuplaj.out");
#endif

using ll = long long;

//#define int ll
#define sz(x) ((int)(x).size())

using pii = pair<int,int>;
using tii = tuple<int,int,int>;

const int nmax = 1e5 + 5;

vector<int> g[nmax];
int l[nmax], r[nmax];
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

int viz[nmax];
bool push(int node) {
  if(viz[node] == 1) return 0;
  viz[node] = 1;
  for(auto x : g[node]) {
    if(l[x] == -1) {
      r[node] = x;
      l[x] = node;
      return 1;
    }
    //cerr << x << ' ' << node << '\n';
  }
  
  for(auto x : g[node]) {
    if(push(l[x])) {
      r[node] = x;
      l[x] = node;
      return 1;
    }
  }
  return 0;
}

signed main() {
  int n, m, E;
  cin >> n >> m >> E;
  vector<pii> edg(E);
  for(auto &[a, b] : edg) cin >> a >> b;
  random_shuffle(all(edg), [&](int x) { return rng() % x; });
  for(auto [a, b] : edg)
    --a,
    --b,
    g[a].emplace_back(b + n);
  int u = n;
  n += m; // idk
  m = u;
  for(int i = 0; i < n; i++)
    l[i] = -1, r[i] = -1;
  
  bool ok;
  do {
    ok = 0;
    memset(viz, 0, sizeof(viz[0]) * (n + 1));
    for(int i = 0; i < n; i++)
      if(r[i] == -1)
        ok |= push(i);
  } while(ok);
  
  vector<pii> rez;
  for(int i = 0; i < n; i++) {
    if(r[i] != -1)
      rez.emplace_back(i, r[i]);
  }
  cout << sz(rez) << '\n';
  for(auto [a, b] : rez)
    cout << a + 1 << ' ' << b - m + 1 << '\n';
  
}

/**
      Vom spune că toamna a venit... foarte trist -
-- George Bacovia, Scântei galbene
*/