Cod sursa(job #2553446)

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


int n, m, e, corespondent[2*nmax];
vector <int> graph[nmax];
bool fumat[2*nmax];

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(corespondent[var] != nod)
        if(cupleaza(corespondent[var]))
        {
            corespondent[var] = nod;
            corespondent[nod] = var;
            return 1;
        }
    }
    return 0;
}

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