Cod sursa(job #1163681)

Utilizator cristitamasTamas Cristian cristitamas Data 1 aprilie 2014 16:03:56
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.23 kb
#include <iostream>
#include <cstdio>
#include <bitset>
#include <vector>
#define Nmax 10010
using namespace std;

int N,M;
int R[Nmax],L[Nmax];

int Nr;

vector <int> Graf[Nmax];
bitset <Nmax> Viz;

void Citire()
{
    int x,y,e;
    scanf("%d %d %d",&N,&M,&e);
    for(int i=0;i<e;++i)
    {
        scanf("%d %d",&x,&y);
        Graf[x].push_back(y);
    }
}

bool Cuplaj(int x)
{
    if(Viz[x])
        return false;
    Viz[x]=1;
    for(int i=0;i<Graf[x].size();++i)
    {
        int v=Graf[x][i];
        if(!R[v] || Cuplaj(R[v]))
        {
            L[x]=v;
            R[v]=x;
            return true;
        }
    }
    return false;
}

void Rezolvare()
{
    bool ok=true;
    while(ok)
    {
        ok=0;
        Viz=0;
        for(int i=1;i<=N;++i)
        {
            if(!L[i] && Cuplaj(i))
            {
                ok=1;
                Nr++;
            }
        }
    }
}

void Afisare()
{
    printf("%d\n",Nr);
    for(int i=1;i<=N;++i)
        if(L[i])
            printf("%d %d\n",i,L[i]);
}

int main()
{
    freopen("cuplaj.in","r",stdin);
    freopen("cuplaj.out","w",stdout);
    Citire();
    Rezolvare();
    Afisare();
    return 0;
}