Cod sursa(job #1790595)

Utilizator gabrielinelusGabriel-Robert Inelus gabrielinelus Data 28 octombrie 2016 14:58:29
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.28 kb
#include <bits/stdc++.h>

using namespace std;

const int Nmax = 66013;

int L[Nmax], R[Nmax], Left, Right, m;
vector<int> G[Nmax];
bitset<Nmax> used;

bool cuplaj(int k)
{
    if(used[k])
        return 0;
    used[k] = 1;
    for(auto it : G[k])
        if(L[it] == 0) {
            R[k] = it;
            L[it] = k;
            return true;
        }

    for(auto it : G[k])
        if(cuplaj(L[it]))
        {
            R[k] = it;
            L[it] = k;
            return true;
        }
    return 0;
}

int main()
{
    freopen("cuplaj.in","r",stdin);
    freopen("cuplaj.out","w",stdout);

    cin.sync_with_stdio(false);

    cin >> Left >> Right >> m;

    for(int i = 1; i <= m; ++i)
    {
        int a,b;
        cin >> a >> b;
        G[a].push_back(Left + b);
        G[Left + b].push_back(a);
    }

    int result = 0;
    bool notDone = true;
    while(notDone) {
        notDone = false;
        used = 0;
        for(int i = 1; i <= Left; ++i)
            if(!R[i] && cuplaj(i))
            {
                ++result;
                notDone = true;
            }
    }
    cout << result << "\n";
    for(int i = 1; i <= Left; ++i)
        if(R[i])
            cout << i << " " << R[i] - Left << "\n";

    return 0;
}