Cod sursa(job #1398282)

Utilizator irimiecIrimie Catalin irimiec Data 24 martie 2015 08:55:15
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.72 kb
#include <bits/stdc++.h>

using namespace std;

#define     mp              make_pair
#define     fs              first
#define     sc              second
#define     pob             pop_back
#define     pub             push_back
#define     eps             1E-7
#define     sz(a)           a.size()
#define     count_one       __builtin_popcount;
#define     count_onell     __builtin_popcountll;
#define     fastIO          ios_base::sync_with_stdio(false)
#define     PI              (acos(-1.0))
#define     linf            (1LL<<62)//>4e18
#define     inf             (0x7f7f7f7f)//>2e9

#define MAXN 10010

#ifndef ONLINE_JUDGE
ifstream f("cuplaj.in");
ofstream g("cuplaj.out");
#endif

int n, m, e;
vector<int> p1[MAXN];
int l[MAXN], r[MAXN];
bitset<MAXN> viz;

bool pairup(int nod) {
    if(viz[nod])
        return false;
    viz[nod] = true;
    for(auto it : p1[nod]) {
        if(!r[it]) {
            l[nod] = it;
            r[it] = nod;
            return true;
        }
    }
    for(auto it : p1[nod]) {
        if(pairup(r[it])) {
            l[nod] = it;
            r[it] = nod;
            return true;
        }
    }
    return false;
}

int main() {
    f >> n >> m >> e;

    int a, b;
    for(int i = 0; i < e; ++i) {
        f >> a >> b;
        p1[a].pub(b);
    }

    bool ok = true;
    while(ok) {
        ok = false;
        viz.reset();
        for(int i = 1; i <= n; ++i)
            if(!l[i])
                ok |= pairup(i);
    }

    int cnt = 0;
    for(int i = 1; i <= n; ++i)
        if(l[i])
            cnt++;
    g << cnt << "\n";

    for(int i = 1; i <= n; ++i)
        if(l[i])
            g << i << " " << l[i] << "\n";
    return 0;
}