Cod sursa(job #582808)

Utilizator jupanubv92Popescu Marius jupanubv92 Data 16 aprilie 2011 00:06:30
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.33 kb
#include <cstdio>
#include <cstring>
#include <fstream>
#define Nmx 10001

using namespace std;

int L[Nmx],R[Nmx],viz[Nmx],n,m;
struct nod
{
    int inf;
    nod *urm;
};
nod *G[Nmx];

ifstream fin("cuplaj.in");

void add(int x,int y)
{
    nod *aux=new nod;
    aux->inf = y;
    aux->urm = G[x];
    G[x] = aux;
}

void read()
{
    int t,x,y;
    fin>>n>>m>>t;
    for(;t;t--)
    {
        fin>>x>>y;
        add(x,y);
    }
}

int pairup(int x)
{
    if(viz[x])
        return 0;
    viz[x]=1;
    for(nod *p=G[x];p;p=p->urm)
        if(!R[p->inf])
        {
            R[p->inf]=x;
            L[x]=p->inf;
            return 1;
        }
    for(nod *p=G[x];p;p=p->urm)
        if(pairup(R[p->inf]))
        {
            R[p->inf]=x;
            L[x]=p->inf;
            return 1;
        }
    return 0;
}

void solve()
{
    int ok,cuplaj=0;
    do
    {
        ok=0;
        memset(viz,0,sizeof(viz));
        for(int i=1;i<=n;++i)
            if(!L[i]&&pairup(i))
            {
                cuplaj++;
                ok=1;
            }
    }while(ok);
    printf("%d\n",cuplaj);
    for(int i=1;i<=n;++i)
        if(L[i])
            printf("%d %d\n",i,L[i]);
}

int main()
{
    freopen("cuplaj.out","w",stdout);
    read();
    solve();
    return 0;
}