Cod sursa(job #2767618)

Utilizator Edyci123Bicu Codrut Eduard Edyci123 Data 6 august 2021 23:34:01
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.19 kb
#include <bits/stdc++.h>
#define NMAX 10005

using namespace std;

ifstream f("cuplaj.in");
ofstream g("cuplaj.out");

int n, m, l[NMAX], r[NMAX], ans, M;
bitset <NMAX> v;
vector <int> edges[NMAX];

bool cupleaza(int nod)
{
    if(v[nod])
        return 0;
    v[nod] = 1;

    for(auto k : edges[nod])
        if(!r[k])
        {
            l[nod] = k;
            r[k] = nod;
            return 1;
        }
    for(auto k : edges[nod])
        if(cupleaza(r[k]))
        {
            l[nod] = k;
            r[k] = nod;
            return 1;
        }
    return 0;
}

int main()
{
    f >> n >> m >> M;

    for(int i = 1; i <= M; i++)
    {
        int x, y;
        f >> x >> y;
        edges[x].push_back(y);
    }

    bool ok;
    do
    {
        ok = 0;
        for(int i = 1; i <= n; i++)
            v[i] = 0;

        for(int i = 1; i <= n; i++)
            if(l[i] == 0)
                ok = ok | cupleaza(i);

    }while(ok == 1);

    for(int i = 1; i <= n; i++)
        if(l[i])
            ans++;
    g << ans << "\n";

    for(int i = 1; i <= n; i++)
        if(l[i])
            g << i << " " << l[i] << "\n";

    return 0;
}