Cod sursa(job #2229325)

Utilizator YouDontNeedMyNameJurcut Paul YouDontNeedMyName Data 6 august 2018 16:05:50
Problema Cuplaj maxim in graf bipartit Scor 24
Compilator cpp Status done
Runda Arhiva educationala Marime 1.26 kb
#include <bits/stdc++.h>

using namespace std;
ifstream in("cuplaj.in");
ofstream out("cuplaj.out");
vector <int> lista[10005];
int n,m,e,l[10005],r[10005],v[10005];
bool cuplaj(int node)
{
    if(v[node])
    {
        return 0;
    }
    v[node]=1;
    for(int i=0; i<lista[node].size(); i++)
    {
        if(l[lista[node][i]]==0)
        {
            r[node]=lista[node][i];
            l[lista[node][i]]=node;
            return 1;
        }
    }
    for(int i=0; i<lista[node].size(); i++)
    {
        if(cuplaj(lista[node][i]))
        {
            r[node]=lista[node][i];
            l[lista[node][i]]=node;
            return 1;
        }
    }
    return 0;
}
int main()
{
    in >> n >> m >> e;
    for(int i=1; i<=e; i++)
    {
        int x,y;
        in >> x >> y;
        lista[x].push_back(y);
    }
    int ok=1;
    while(ok)
    {
        ok=0;
        for(int i=1; i<=n; i++)
        {
            if(r[i]==0) ok|=cuplaj(i);
        }
        memset(v,0,sizeof(v));
    }
    int nr=0;
    for(int i=1; i<=n; i++)
    {
        if(r[i])
            nr++;
    }
    out << nr << '\n';
    for(int i=1; i<=n; i++)
    {
        if(r[i])
            out << i << ' ' << r[i] << '\n';
    }
    return 0;
}