Cod sursa(job #2553457)

Utilizator flee123Flee Bringa flee123 Data 21 februarie 2020 23:18:43
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.51 kb
#include <bits/stdc++.h>
#define nmax 10005
using namespace std;
ifstream fin("cuplaj.in");
ofstream fout("cuplaj.out");

int n, m, e, corespondent[20100];
vector <int> graph[10100];
bool fumat[20100];

bool cupleaza(int nod)
{
    if(fumat[nod] == 1) return 0;
    fumat[nod] = 1;
    unsigned i, len = graph[nod].size();
    int var;
    for(i = 0; i < len; i++)
    {
        var = graph[nod][i];
        if(!corespondent[var])
        {
            corespondent[var] = nod;
            corespondent[nod] = var;
            return 1;
        }
    }
    for(i = 0; i < len; i++)
    {
        var = graph[nod][i];
        if(cupleaza(corespondent[var]))
        {
            corespondent[var] = nod;
            corespondent[nod] = var;
            return 1;
        }
    }
    return 0;
}

int main()
{
    int i, ok = 1, x, y, raspuns = 0;
    fin >> n >> m >> e;
    for(i = 0; i < e; i++)
    {
        fin >> x >> y;
        y += n;
        graph[x].push_back(y);
    }
    while(ok)
    {
        ok = 0;
        for(i = 1; i <= n; i++) fumat[i] = 0;
        for(i = 1; i <= n; i++)
        {
            if(corespondent[i] == 0){
            y = cupleaza(i);
            if(y)
            {
                ++raspuns;
                ok = 1;
            }
            }
        }
    }
    fout << raspuns << '\n';
    for(i = 1; i <= n; i++)
    {
        if(corespondent[i] != 0)
            fout << i << ' ' << corespondent[i] - n << '\n';
    }
    return 0;
}