Cod sursa(job #2416407)

Utilizator alex2kamebossPuscasu Alexandru alex2kameboss Data 27 aprilie 2019 15:19:29
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.12 kb
#include <iostream>
#include <cstdio>
#include <vector>
#include <bitset>

#define N 10005

using namespace std;

int n,m,e,x,y;
vector <int> g[N];
vector <pair<int,int>> sol;
int st[N], dr[N];
bitset <N> viz;

bool cuplaj(int nod){
    if(viz[nod])
        return 0;
    viz[nod] = 1;
    for(int i : g[nod])
        if(!dr[i]){
            dr[i] = nod;
            st[nod] = i;
            return 1;
        }

    for(int i : g[nod])
        if(cuplaj(dr[i])){
            dr[i] = nod;
            st[nod] = i;
            return 1;
        }

    return 0;
}

int main()
{
    freopen("cuplaj.in","r",stdin);
    freopen("cuplaj.out","w",stdout);

    scanf("%d%d%d", &n,&m,&e);
    for(int i = 0; i < e; ++i){
        scanf("%d%d", &x,&y);
        g[x].push_back(y);
    }

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

    for(int i = 1; i <= n; ++i)
        if(st[i])
            sol.push_back({i,st[i]});

    cout<<sol.size()<<"\n";
    for(auto i : sol)
        cout<<i.first<<" "<<i.second<<"\n";

    return 0;
}