Cod sursa(job #3349154)

Utilizator iulia_morariuIuli Morariu iulia_morariu Data 25 martie 2026 18:06:14
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.32 kb
#include <algorithm>
#include <iostream>
#include <fstream>
#include <climits>
#include <vector>
#include <stack>
#include <cmath>
#include <queue>
// #include <bits/std++.h>
#define in  fin
#define out fout

using namespace std;
const int NMAX = 1e5 + 5;

ifstream fin("cuplaj.in");
ofstream fout("cuplaj.out");

vector<int> g[NMAX];
int cuplaj[NMAX];
bool mrc[NMAX];

bool pair_up(int nod){
    if(mrc[nod]) return 0;
    mrc[nod] = 1;

    for(const int &cop : g[nod]){
        if(!cuplaj[cop] || pair_up( cuplaj[cop] )){
            cuplaj[cop] = nod;
            cuplaj[nod] = cop;
            return 1;
        }
    }
    return 0;
}

signed main(){
    ios_base::sync_with_stdio(false);
    in.tie(NULL);

    int n, m, e; in >> n >> m >> e;
    for(int i = 0; i < e; i++){
        int x, y; in >> x >> y;
        g[x].push_back(y + n);
    }

    bool berezelius_ciorba = 1;
    int total = 0;
    while(berezelius_ciorba){
        berezelius_ciorba = 0;
        for(int i = 1; i <= n; i++) mrc[i] = 0;
        for(int i = 1; i <= n; i++){
            if(!cuplaj[i] && pair_up(i)){
                total++;
                berezelius_ciorba = 1;
            }
        }
    }

    out << total << '\n';
    for(int i = 1; i <= n; i++){
        if(cuplaj[i]){
            out << i << " " << cuplaj[i] - n << '\n';
        }
    }

    return 0;
}