Cod sursa(job #2489325)

Utilizator uvIanisUrsu Ianis Vlad uvIanis Data 8 noiembrie 2019 15:18:20
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.27 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin{"cuplaj.in"};
ofstream fout{"cuplaj.out"};

int N, M, E;
vector<vector<int>> graph(10001, vector<int>());

int L[10001], R[10001];
bool used[10001];

bool cuplaj(int node)
{
    used[node] = true;

    for(auto& next : graph[node])
        if(R[next] == 0)
        {
            R[next] = node;
            L[node] = next;
            return true;
        }

    for(auto& next : graph[node])
        if(used[R[next]] == false && cuplaj(R[next]) == true)
        {
            R[next] = node;
            L[node] = next;
            return true;
        }

    return false;
}

int main()
{

    fin >> N >> M >> E;

    for(int i = 1, x, y; i <= E; ++i)
    {
        fin >> x >> y;
        graph[x].push_back(y);
    }

    bool ok = true;

    while(ok)
    {
        fill(used + 1, used + N + 1, false);

        ok = false;

        for(int i = 1; i <= N; ++i)
            if(used[i] == false && L[i] == 0)
                ok |= cuplaj(i);
    }

    int ans = 0;

    for(int i = 1; i <= N; ++i)
        if(L[i] != 0)
            ans++;

    fout << ans << '\n';

    for(int i = 1; i <= N; ++i)
        if(L[i] != 0)
            fout << i << ' ' << L[i] << '\n';
}