Cod sursa(job #2301420)

Utilizator YouDontNeedMyNameJurcut Paul YouDontNeedMyName Data 12 decembrie 2018 22:36:26
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 in("cuplaj.in");
ofstream out("cuplaj.out");
int n,m,e,viz[10005],stanga[10005],dreapta[10005],sol[10005];
vector <int> lista[10005];
bool gasesc_cuplaj(int node)
{
    if(viz[node]) return false;
    viz[node]=1;
    for(auto x:lista[node])
    {
        if(!stanga[x] || gasesc_cuplaj(stanga[x]))
        {
            stanga[x]=node;
            dreapta[node]=x;
            return true;
        }
    }
    return false;
}
int main()
{
    in >> n >> m >> e;
    for(int i=1; i<=e; i++)
    {
        int x,y;
        in >> x >> y;
        lista[x].push_back(y);
    }
    bool ok=true;
    while(ok)
    {
        ok=false;
        memset(viz,0,sizeof(viz));
        for(int i=1; i<=n; i++)
        {
            if(!dreapta[i])
            {
                ok|=gasesc_cuplaj(i);
            }
        }
    }
    int nr=0;
    for(int i=1; i<=n; i++)
    {
        if(dreapta[i])
            sol[++nr]=i;
    }
    out << nr << '\n';
    for(int i=1; i<=nr; i++)
    {
        out << sol[i] << ' ' << dreapta[sol[i]] << '\n';
    }
    return 0;
}