Cod sursa(job #3373811)

Utilizator Chris_BlackBlaga Cristian Chris_Black Data 7 noiembrie 2026 22:27:29
Problema Cuplaj maxim in graf bipartit Scor 28
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.77 kb
#include <bits/stdc++.h>

#define ll long long
#define vi vector<int>
#define vl vector<ll>
#define vvi vector<vi>
#define vvl vector<vl>
#define pii pair<int, int>
#define ff first
#define ss second
#define vpi vector<pii>
#define FOR(i, a, b) for(int i = (a); i <= (b); ++i)
#define FORR(i, a, b) for(int i = (a); i >= (b); --i)
#define pb push_back
using namespace std;
const int N = 1e4 + 9;
const bool testcases = false;

const string INPUT_FILE = "cuplaj";
ifstream fin(INPUT_FILE + ".in");
ofstream fout(INPUT_FILE + ".out");
#define cin fin
#define cout fout

int n, m, e;

vvi G(N);
int l[N], r[N];
bitset<N> viz;
bool Match(int x) {
    if (viz[x]) return false;
    viz[x] = true;

    for (auto y : G[x]) {
        if (!r[y]) {
            l[x] = y;
            r[y] = x;
            return true;
        }
    }

    for (auto y : G[x]) {
        if (Match(y)) {
            l[x] = y;
            r[y] = x;
            return true;
        }
    }

    return false;
}

int MaxMatch() {
    int ret = 0;

    bool found_path;
    do {
        found_path = false;
        viz.reset();

        FOR(i, 1, n) {
            if (!l[i] && Match(i)) {
                found_path = true;
                ret ++;
            }
        }
    } while (found_path);
    return ret;
}

void solve_testcase() {
    cin >> n >> m >> e;

    {
        int x, y;
        FOR(i, 1, e) {
            cin >> x >> y;
            G[x].pb(y);
        }
    }

    cout << MaxMatch() << '\n';
    FOR(i, 1, n) if (l[i]) cout << i << ' ' << l[i] << '\n';
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);

    int t = 1;
    if (testcases) cin >> t;
    while (t --) solve_testcase();
    return 0;
}