Cod sursa(job #1843041)

Utilizator vladm98Munteanu Vlad vladm98 Data 8 ianuarie 2017 00:05:26
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.27 kb
#include <bits/stdc++.h>

using namespace std;
vector <int> graf[10001];
int boss_left[10001], boss_right[10001], viz[10001], rasp;
bool cuplaj (int nod)
{
    if (viz[nod] == 1)
        return 0;
    viz[nod] = 1;
    for (auto x:graf[nod])
    {
        if (boss_right[x] == 0)
        {
            boss_left[nod] = x;
            boss_right[x] = nod;
            return 1;
        }
    }
    for (auto x:graf[nod])
    {
        if (cuplaj(boss_right[x]))
        {
            boss_left[nod] = x;
            boss_right[x] = nod;
            return 1;
        }
    }
    return 0;
}
int main()
{
    int n, m, x, y, k;
    ifstream fin ("cuplaj.in");
    ofstream fout ("cuplaj.out");
    fin >> n >> m >> k;
    for (int i = 0; i<k; ++i)
    {
        fin >> x >> y;
        graf[x].push_back(y);
    }
    for (int c = 1; c;)
    {
        c = 0;
        memset(viz, 0, sizeof(viz));
        for (int i = 1; i<=n; ++i){
            if (boss_left[i] == 0)
                c|=cuplaj(i);
        }
    }
    for (int i = 1; i<=n; ++i)
        if (boss_left[i])
            ++rasp;
    fout << rasp << '\n';
    for (int i = 1; i<=n; ++i)
        if (boss_left[i])
            fout << i << " " << boss_left[i] << '\n';
    return 0;
}