Cod sursa(job #2674924)

Utilizator bem.andreiIceman bem.andrei Data 20 noiembrie 2020 19:15:57
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.22 kb
#include <bits/stdc++.h>

using namespace std;
ifstream r("cuplaj.in");
ofstream w("cuplaj.out");
vector<int>g[10005];
bool viz[10005];
int st[10005], dr[10005];
bool cuplaj(int a){
    if(viz[a]==1){
        return false;
    }
    viz[a]=1;
    for(auto it: g[a]){
        if(dr[it]==0){
            dr[it]=a;
            st[a]=it;
            return true;
        }
    }
    for(auto it: g[a]){
        if(cuplaj(dr[it])==1){
            dr[it]=a;
            st[a]=it;
            return true;
        }
    }
    return false;
}
int main()
{
    int n, m, e;
    r>>n>>m>>e;
    for(int i=0;i<e;i++){
        int x, y;
        r>>x>>y;
        g[x].push_back(y);
    }
    bool ok=false;
    while(ok==false){
        memset(viz, 0, sizeof(viz));
        ok=true;
        for(int i=1;i<=n;i++){
            if(st[i]!=0){
                continue;
            }
            if(cuplaj(i)==1){
                ok =false;
            }
        }
    }
    int cnt=0;
    for(int i=1;i<=n;i++){
        if(st[i]){
            cnt++;
        }
    }
    w<<cnt<<"\n";
    for(int i=1;i<=n;i++){
        if(st[i]!=0){
            w<<i<<" "<<st[i]<<"\n";
        }
    }
    return 0;
}