Cod sursa(job #2262571)

Utilizator cezarzbughinCezar Zbughin cezarzbughin Data 17 octombrie 2018 17:00:08
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.14 kb
#include <bits/stdc++.h>

using namespace std;
ifstream f("cuplaj.in");
ofstream g("cuplaj.out");
const int N = 10010;
vector<int> v[N];
bitset<N> viz;
int direct[N],invers[N];
int n,m,e,x,y;
bool cuplez(int);
int main()
{
    f>>n>>m>>e;
    for(; e; e--)
    {
        f>>x>>y;
        v[x].push_back(y);
    }
    bool ok=true;
    int cuplari=0;
    while(ok)
    {
        ok=false;
        viz.reset();
        for(int i=1; i<=n; i++)
            if(!direct[i])
                if(cuplez(i))
                {
                    ok=true;
                    cuplari++;
                }
    }
    g<<cuplari<<'\n';
    for(int i=1;i<=n;i++)
        if(direct[i])
            g<<i<<' '<<direct[i]<<'\n';
    return 0;
}
bool cuplez(int st)
{
    if(viz[st])return false;
    viz[st]=1;
    for(auto dr:v[st])
        if(!invers[dr])
        {
            direct[st]=dr;
            invers[dr]=st;
            return true;
        }
    for(auto dr:v[st])
        if(cuplez(invers[dr]))
        {
            direct[st]=dr;
            invers[dr]=st;
            return true;
        }
    return false;
}