Cod sursa(job #3208070)

Utilizator amcbnCiobanu Andrei Mihai amcbn Data 27 februarie 2024 17:23:48
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.54 kb
#include <bits/stdc++.h>
using namespace std;
const char nl = '\n';
const char sp = ' ';
const int inf = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const char out[2][4]{ "NO", "YES" };
#define all(a) a.begin(), a.end()
using ll = long long;
ifstream fin("cuplaj.in");
ofstream fout("cuplaj.out");

const int nmax = 2e4;
int n1, n2, m;
vector<int> g[nmax + 5];
bool vis[nmax + 5]{ 0 };
int match[nmax + 5]{ 0 };

bool solve(int u) {
    vis[u] = true;
    for (auto& v : g[u]) {
        if (match[v] == 0) {
            match[v] = u;
            match[u] = v;
            return true;
        }
        if (vis[match[v]]) {
            continue;
        }
        if (solve(match[v])) {
            match[v] = u;
            match[u] = v;
            return true;
        }
    }
    return false;
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    fin >> n1 >> n2 >> m;
    for (int i = 1; i <= m; ++i) {
        int u, v;
        fin >> u >> v;
        g[u].push_back(v + n1);
        g[v + n1].push_back(u);
    }
    if (n1 <= n2) {
        for (int i = 1; i <= n1; ++i) {
            fill(vis + 1, vis + n1 + 1, 0);
            solve(i);
        }
    }
    else {
        for (int i = n1 + 1; i <= n1 + n2; ++i) {
            fill(vis + n1 + 1, vis + n1 + n2 + 1, 0);
            solve(i);
        }
    }
    fout << n1 - count(match + 1, match + n1 + 1, 0) << nl;
    for (int i = 1; i <= n1; ++i) {
        if (match[i] != 0) {
            fout << i << sp << match[i] - n1 << nl;
        }
    }
}