Cod sursa(job #1500226)

Utilizator stefanzzzStefan Popa stefanzzz Data 11 octombrie 2015 17:13:53
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.19 kb
#include <stdio.h>
#include <vector>
#include <string.h>
#define MAXN 10005
using namespace std;

bool uz[MAXN];
int n, m, e, x, y, L[MAXN], R[MAXN];
vector<int> G[MAXN];

bool pairUp(int u) {
    if(uz[u]) return 0;
    uz[u] = 1;
    for(auto x: G[u])
        if(!R[x]) {
            L[u] = x;
            R[x] = u;
            return 1;
        }

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

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

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

    bool done = 0;
    while(!done) {
        done = 1;
        memset(uz, 0, sizeof(uz));
        for(int i = 1; i <= n; i++)
            if(!L[i] && pairUp(i))
                done = 0;
    }

    vector< pair<int, int> > sol;
    for(int i = 1; i <= n; i++)
        if(L[i])
            sol.push_back(make_pair(i, L[i]));

    printf("%d\n", sol.size());
    for(auto x: sol)
        printf("%d %d\n", x.first, x.second);

    return 0;
}