Cod sursa(job #3225480)

Utilizator maiaauUngureanu Maia maiaau Data 17 aprilie 2024 18:05:53
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.16 kb
#include <bits/stdc++.h>
using namespace std;
using pii = pair<int,int>;
using ll = long long;
#define pb push_back

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

const int N = 2e4+5;

int n, cnt, cup[N];
vector<int> e[N];
bitset<N> viz;

void read();
bool cupleaza(int);

int main()
{
    read();
    bool c = 1;
    while (c){
        c = 0;
        viz.reset();
        for (int i = 1; i <= n; i++)
            if (!cup[i] && !viz[i])
                if (cupleaza(i))
                    c = 1, cnt++;
    }
    fout << cnt << '\n';
    for (int i = 1; i <= n; i++)
        if (cup[i]) fout << i << ' ' << cup[i] - n << '\n';
    
    return 0;
}

void read(){
    int m, cnte; fin >> n >> m >> cnte;
    while (cnte--){
        int a, b; fin >> a >> b; b += n;
        e[a].pb(b); e[b].pb(a);
    }
}
bool cupleaza(int nod){
    if (viz[nod]) return 0;
    viz[nod] = 1;
    for (auto it: e[nod])
        if (!cup[it]){
            cup[it] = nod;
            cup[nod] = it;
            return 1;
        }
    for (auto it: e[nod])
        if (cupleaza(cup[it])){
            cup[nod] = it;
            cup[it] = nod;
            return 1;
        }
    return 0;
}

//17:58