Cod sursa(job #2207230)

Utilizator giotoPopescu Ioan gioto Data 25 mai 2018 11:32:37
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.08 kb
#include <bits/stdc++.h>
using namespace std;

int n, m, e;
bool f[10005];
int l[10005], r[10005];
vector <int> v[10005];
inline bool cuplaj(int nod){
    if(f[nod]) return 0;
    f[nod] = 1;
    for(auto it : v[nod]){
        if(!r[it]){
            l[nod] = it;
            r[it] = nod;
            return 1;
        }
    }
    for(auto it : v[nod]){
        if(cuplaj(r[it])){
            l[nod] = it;
            r[it] = nod;
            return 1;
        }
    }
    return 0;
}
int main()
{
    freopen("cuplaj.in", "r", stdin);
    freopen("cuplaj.out", "w", stdout);

    scanf("%d%d%d", &n, &m, &e);
    int x, y;
    for(int i = 1; i <= e ; ++i){
        scanf("%d%d", &x, &y);
        v[x].push_back(y);
    }

    bool ok;
    do{
        ok = 0;
        memset(f, 0, sizeof(f));
        for(int i = 1; i <= n ; ++i) if(!l[i]) ok |= cuplaj(i);

    }while(ok);

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

    return 0;
}