Cod sursa(job #2906281)

Utilizator teo1496Teodor Juravlea teo1496 Data 25 mai 2022 14:53:28
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.16 kb
#include <bits/stdc++.h>
#define INF 99999999
 
using namespace std;
 
ifstream fin("cuplaj.in");
ofstream fout("cuplaj.out");
 
int n, m, e, x, y, ans, st[100005], d[100005];
vector<int> G[100005];
bitset<100005> v;
 
bool cuplaj(int nod){
    if(v[nod]){
        return false;
    }
    v[nod] = true;
    for(auto i : G[nod]){
        if(d[i] == 0){
            st[nod] = i;
            d[i] = nod;
            return true;
        }
    }
    for(auto i : G[nod]){
        if(cuplaj(d[i])){
            st[nod] = i;
            d[i] = nod;
            return true;
        }
    }
    return false;
}
 
int main(){
    fin >> n >> m >> e;
    for(int i = 1; i <= e; i++){
        fin >> x >> y;
        G[x].push_back(y);
    }
    fin.close();

    bool ok = true;

    while(ok){
        ok = false;
        v.reset();
        for(int i = 1; i <= n; i++){
            if(st[i] == 0) {
                ok |= cuplaj(i);
            }
        }
    }

    for(int i = 1; i <= n; i++){
        ans += (st[i] > 0);
    }

    fout << ans << "\n";
    for(int i = 1; i <= n; i++){
        if(st[i] != 0){
            fout << i << " " << st[i] << "\n";
        }
    }
    return 0;
}