Cod sursa(job #1389701)

Utilizator gabib97Gabriel Boroghina gabib97 Data 16 martie 2015 15:47:00
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.17 kb
#include <stdio.h>
#include <vector>
#include <stdlib.h>
#include <string.h>
using namespace std;
int n,m,e,i,x,y,p1[10001],p2[10001],gasit,cuplaj,o[10001];
vector<int> G[10001];
int match(int s)
{
    int i,z=G[s].size();
    if (o[s]) return 0;
    o[s]=1;
    for (i=0;i<z;i++)
        if (!p2[G[s][i]])
        {
            p1[s]=G[s][i];
            p2[G[s][i]]=s;
            return 1;
        }
    for (i=0;i<z;i++)
        if (match(p2[G[s][i]]))
        {
            p1[s]=G[s][i];
            p2[G[s][i]]=s;
            return 1;
        }
    return 0;
}
int main()
{
    freopen ("cuplaj.in","r",stdin);
    freopen ("cuplaj.out","w",stdout);
    scanf("%i%i%i",&n,&m,&e);
    for (i=1;i<=e;i++)
    {
        scanf("%i%i",&x,&y);
        G[x].push_back(y);
    }
    gasit=1;
    while (gasit)
    {
        gasit=0;
        memset(o,0,sizeof(o));
        for (i=1;i<=n;i++)
            if (!p1[i]) gasit+=match(i);
    }
    for (i=1;i<=n;i++)
        if (p1[i]) cuplaj++;
    printf("%i\n",cuplaj);
    for (i=1;i<=n;i++)
        if (p1[i]) printf("%i %i\n",i,p1[i]);
    fclose(stdin);
    fclose(stdout);
    return 0;
}