Cod sursa(job #1290567)

Utilizator Al3ks1002Alex Cociorva Al3ks1002 Data 11 decembrie 2014 15:03:53
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.35 kb
#include<cstdio>
#include<fstream>
#include<iostream>
#include<iomanip>
#include<algorithm>
#include<vector>
#include<bitset>
#include<deque>
#include<queue>
#include<set>
#include<map>
#include<cmath>
#include<cstring>
#include<ctime>
#include<cstdlib>
#include<unordered_map>

#define ll long long
#define pb push_back
#define mp make_pair
#define pii pair<int,int>
#define pll pair<ll,ll>

using namespace std;

const int nmax = 10005;

int n, m, e, i, x, y, ok, sol, l[nmax], r[nmax];

vector<int> v[nmax];
bitset<nmax> viz;

bool pairup(int x)
{
    if(viz[x]) return 0;
    viz[x] = 1;

    for(auto it : v[x])
        if(!r[it] || pairup(r[it]))
        {
            l[x] = it;
            r[it] = x;
            return 1;
        }

    return 0;
}

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

    scanf("%d%d%d", &n, &m, &e);

    for(; e; e--)
    {
        scanf("%d%d", &x, &y);
        v[x].pb(y);
    }

    for(ok = 1; ok;)
    {
        ok = 0;
        viz = 0;
        for(i = 1; i <= n; i++)
            if(!l[i] && pairup(i))
            {
                sol++;
                ok = 1;
            }
    }

    printf("%d\n", sol);
    for(i = 1; i <= n; i++)
        if(l[i])
            printf("%d %d\n", i, l[i]);

    return 0;
}