Cod sursa(job #3298719)

Utilizator nstefanNeagu Stefan nstefan Data 31 mai 2025 23:52:45
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.22 kb
#include <bits/stdc++.h>
#define int long long
#define ld long double
using namespace std;

// Inspired by: https://infoarena.ro/job_detail/3297319?action=view-source

int L, R, M;
int p[100'010];
bool viz[100'010];
vector<int> adj[100'010];

bool pair_up(int node) {
  if (viz[node]) return false;
  viz[node] = true;

  for (auto child : adj[node]) {
    if (!p[child] || pair_up(p[child])) {
      p[node] = child;
      p[child] = node;
      return true;
    }
  }

  return false;
}

int32_t main() {
  cin.tie(0)->sync_with_stdio(0);
#ifdef N257
freopen("input.txt", "r", stdin);
#else
freopen("cuplaj.in", "r", stdin);
freopen("cuplaj.out", "w", stdout);
#endif

  // Setup the graph
  cin >> L >> R >> M;
  for (int i = 0; i < M; i++) {
    int leftNode, rightNode; cin >> leftNode >> rightNode;
    rightNode += L;
    adj[leftNode].emplace_back(rightNode);
    adj[rightNode].emplace_back(leftNode);
  }

  bool dai = true;
  int cnt = 0;
  while (dai) {
    dai = false;
    memset(viz, 0, sizeof(viz));

    for (int i = 1; i <= L; i++) {
      if (!p[i] && pair_up(i)) {
        cnt++;
        dai = true;
      }
    }
  }

  cout << cnt << '\n';
  for (int i = 1; i <= L; i++)
    if (p[i])
      cout << i << ' ' << p[i] - L << '\n';
}