Cod sursa(job #2825015)

Utilizator DordeDorde Matei Dorde Data 3 ianuarie 2022 20:53:53
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.09 kb
#include <bits/stdc++.h>
using namespace std;
ifstream fin("cuplaj.in");
ofstream fout("cuplaj.out");
int const N = 1e4 + 3;
int n , m , e;
int a , b;
vector<int> v[N];
int l[N] , r[N] , viz[N];
int ans;
int cuplaj(int x){
    if(viz[x])
        return 0;
    viz[x] = 1;
    for(auto y : v[x]){
        if(r[y])
            continue;
        l[x] = y;
        r[y] = x;
        ++ ans;
        return 1;
    }
    for(auto y : v[x]){
        if(cuplaj(r[y])){
            l[x] = y;
            r[y] = x;
            return 1;
        }
    }
    return 0;
}
int main()
{
    fin >> n >> m >> e;
    for(int i = 1 ; i <= e ; ++ i){
        fin >> a >> b;
        v[a].push_back(b);
    }
    while(true){
        fill(viz , viz + 1 + n , 0);
        int r = 0;
        for(int i = 1 ; i <= n ; ++ i)
            if(!l[i])
                r += cuplaj(i);
        if(!r){
            break;
        }
    }
    fout << ans << '\n';
    for(int i = 1 ; i <= m ; ++ i){
        if(r[i]){
            fout << r[i] << ' ' << i << '\n';
        }
    }
    return 0;
}