Cod sursa(job #1439110)

Utilizator stefanzzzStefan Popa stefanzzz Data 21 mai 2015 15:09:09
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.16 kb
#include <stdio.h>
#include <string.h>
#include <vector>
#define MAXN 10005
using namespace std;

int n, m, nre, L[MAXN], R[MAXN];
int x, y;
vector<int> G[MAXN];
bool gata = 0, uz[MAXN];
vector< pair<int, int> > sol;

bool pairUp(int x) {
    if(uz[x]) return 0;
    uz[x] = 1;

    for(auto y: G[x])
        if(!R[y]) {
            L[x] = y;
            R[y] = x;
            return 1;
        }

    for(auto y: G[x])
        if(pairUp(R[y])) {
            L[x] = y;
            R[y] = x;
            return 1;
        }

    return 0;
}

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

    int i;

    scanf("%d %d %d", &n, &m, &nre);
    
    while(nre--) {
        scanf("%d %d", &x, &y);
        G[x].push_back(y);
    }

    while(!gata) {
        gata = 1;
        memset(uz, 0, sizeof(uz));
        for(i = 1; i <= n; i++)
            if(!L[i] && pairUp(i))
                gata = 0;
    }
    
    for(i = 1; i <= n; i++)
        if(L[i])
            sol.push_back(make_pair(i, L[i]));
    
    printf("%d\n", sol.size());
    for(i = 0; i < sol.size(); i++)
        printf("%d %d\n", sol[i].first, sol[i].second);     

    return 0;
}