Cod sursa(job #2603919)

Utilizator TheNextGenerationAyy LMAO TheNextGeneration Data 21 aprilie 2020 12:00:19
Problema Cuplaj maxim in graf bipartit Scor 50
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.09 kb
#include <bits/stdc++.h>

using namespace std;
ifstream in("cuplaj.in");
ofstream out("cuplaj.out");
const int N = 1005;
bool viz[N];
int L[N],R[N];
vector<int> v[N];
bool Match(int x)
{
    if (viz[x])
        return 0;
    viz[x] = 1;
    for (auto it: v[x])
        if (!R[it])
        {
            R[it] = x;
            L[x] = it;
            return 1;
        }
    for (auto it: v[x])
        if (Match(R[it]))
        {
            R[it] = x;
            L[x] = it;
            return 1;
        }
    return 0;
}
int main()
{
    int n,m,e;
    in >> n >> m >> e;
    for (int i = 1; i<=e; i++)
    {
        int x,y;
        in >> x >> y;
        v[x].push_back(y);
    }
    bool ok = 1;
    while (ok)
    {
        ok = 0;
        memset(viz,0,sizeof(viz));
        for (int i = 1; i<=n; i++)
            if (!L[i])
                ok|=Match(i);
    }
    int cnt = 0;
    for (int i = 1; i<=n; i++)
        cnt+=(L[i]>0);
    out << cnt << "\n";
    for (int i = 1; i<=n; i++)
        if (L[i])
            out << i << " " << L[i] << "\n";
}