Cod sursa(job #895922)

Utilizator rootsroots1 roots Data 27 februarie 2013 13:04:04
Problema Cuplaj maxim in graf bipartit Scor 40
Compilator cpp Status done
Runda Arhiva educationala Marime 1.28 kb
#include <cstdio>
#include <iostream>
#include <vector>

#define GLen 10001

using namespace std;

vector < int > g[GLen];

int L[GLen], R[GLen], use[GLen];

inline int cuplaj(int nod)
{
    if(use[nod]) return 0;
    use[nod] = 1;

    for(int i = 0; i < g[nod].size(); ++ i)
        if(R[ g[nod][i] ] == 0)
        {
            L[nod] = g[nod][i];
            R[ g[nod][i] ] = nod;
            return 1;
        }

    for(int i = 0; i < g[nod].size(); ++ i)
        if(cuplaj(R[ g[nod][i] ]))
        {
            L[nod] = g[nod][i];
            R[ g[nod][i] ] = nod;
            return 1;
        }

    return 0;
}

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

    int N, M, E;
    cin >> N >> M >> E;

    for(int i = 1; i <= E; ++ i)
    {
        int x, y;
        cin >> x >> y;

        g[x].push_back(y);
    }

    int change = 1;
    while(change)
    {
        change = 0;

        for(int i = 1; i <= N; ++ i) use[i] = 0;
        for(int i = 1; i <= N; ++ i)
            if(!L[i])
                change |= cuplaj(i);
    }

    int sol = 0;

    for(int i = 1; i <= N; ++ i)
        if(L[i] != 0) ++ sol;

    cout << sol << '\n';

    for(int i = 1; i <= N; ++ i)
        if(L[i])
            cout << L[i] << ' ' << R[i] << '\n';

    return 0;
}