Cod sursa(job #704600)

Utilizator AndrewTheGreatAndrei Alexandrescu AndrewTheGreat Data 2 martie 2012 19:03:15
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.35 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <string.h>
#define pb push_back
#define TR(C, i)\
    for(typeof(C.begin()) i = C.begin(); i < C.end(); i++)

using namespace std;

int N, M, E;
const int nmax = 10100;
vector<int> G[nmax];

void read()
{
    ifstream in( "cuplaj.in" );
    in >> N >> M >> E;
    int i, a, b;
    for(i = 1; i <= E; i++)
    {
        in >> a >> b;
        G[a].pb(b);
    }
}

int L[nmax], R[nmax], U[nmax];

int PairUp(int nod)
{
    if(U[nod]) return 0;
    U[nod] = true;

    TR(G[nod], it)
        if(!R[*it])
        {
            L[nod] = *it;
            R[*it] = nod;
            return 1;
        }

    TR(G[nod], it)
        if(PairUp(R[*it]))
        {
            L[nod] = *it;
            R[*it] = nod;
            return 1;
        }
    return 0;
}

int main()
{
    read();
    int i, cuplaj = 0, ok = true;
    while( ok )
    {
        ok = false;
        memset(U, 0, sizeof(U));

        for(i = 1; i <= N; i++)
            if(!L[i])
                if(PairUp(i))
                {
                    cuplaj++;
                    ok = true;
                }
    }

    ofstream out ("cuplaj.out");
    out << cuplaj << "\n";
    for(i = 1; i <= N; i++)
        if(L[i])
            out << i << " " << L[i] << "\n";
    return 0;
}