Cod sursa(job #3220280)

Utilizator andreiiorgulescuandrei iorgulescu andreiiorgulescu Data 3 aprilie 2024 00:02:47
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.36 kb
#include <bits/stdc++.h>

using namespace std;

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

int n,m,e;
vector<int>g[10005];
int l[10005],r[10005];
bool viz[10005];

bool kuhn(int nod)
{
    if (viz[nod])
        return false;
    viz[nod] = true;
    for (auto vecin : g[nod])
    {
        if (r[vecin] == 0 or kuhn(r[vecin]))
        {
            r[vecin] = nod;
            l[nod] = vecin;
            return true;
        }
    }
    return false;
}

int main()
{
    in >> n >> m >> e;
    for (int i = 1; i <= e; i++)
    {
        int x,y;
        in >> x >> y;
        g[x].push_back(y);
    }
    vector<int>ord;
    for (int i = 1; i <= n; i++)
        ord.push_back(i);
    shuffle(ord.begin(),ord.end(),default_random_engine(12345));
    bool keep_trying = true;
    while (keep_trying)
    {
        memset(viz,0,sizeof(viz));
        keep_trying = false;
        for (auto i : ord)
        {
            if (l[i] != 0)
                continue;
            bool idk = kuhn(i);
            if (idk == true)
                keep_trying = true;
        }
    }
    int cardinal = 0;
    for (int i = 1; i <= n; i++)
        if (l[i])
            cardinal++;
    out << cardinal << '\n';
    for (int i = 1; i <= n; i++)
        if (l[i])
            out << i << ' ' << l[i] << '\n';
    return 0;
}