Cod sursa(job #2920070)

Utilizator BalasaRaduBalasa Radu BalasaRadu Data 21 august 2022 20:27:07
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.33 kb
#include <bits/stdc++.h>//Hopcroft Karp Algorithm
using namespace std;

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

const int dim=1e4+9;

vector<int>v[dim];

int n,m,e;
bool used[dim];
int current_maching_right[dim];
int current_maching_left[dim];

bool try_khun(int x){
    if(used[x]){
        return false;
    }
    used[x]=true;
    for(int y:v[x]){
        if(!current_maching_right[y]||try_khun(current_maching_right[y])){
            current_maching_right[y]=x;
            current_maching_left[x]=y;
            return true;
        }
    }
    return false;
}

signed main(){
        fin>>n>>m>>e;
    for(int i=1;i<=e;i++){
        int x,y;
        fin>>x>>y;
        v[x].push_back(y);
    }

    while(true){
        for(int i=1;i<=n;i++){
            used[i]=false;
        }
        bool ok=false;
        for(int i=1;i<=n;i++){
            if(!current_maching_left[i]&&try_khun(i)){
                ok=true;
            }
        }
        if(!ok){
            break;
        }
    }

    int nr=0;
    for(int i=1;i<=m;i++){
        if(current_maching_right[i]){
            nr++;
        }
    }
        fout<<nr<<'\n';
    for(int i=1;i<=m;i++){
        if(current_maching_right[i]){
            fout<<current_maching_right[i]<<' '<<i<<'\n';
        }
    }
}