Cod sursa(job #1167836)

Utilizator costyv87Vlad Costin costyv87 Data 5 aprilie 2014 23:10:16
Problema Cuplaj maxim in graf bipartit Scor 24
Compilator cpp Status done
Runda Arhiva educationala Marime 1.34 kb
//HighFLow
#include <cstdio>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <vector>
using namespace std;
FILE *f,*g;
#define ll (long long)
#define fcat(c) while (c!='\n') fscanf(f,"%c",&c);
#define cat(c) while(c!='\n') scanf("%c",&c);
vector <int> a[10100];
int L[10100],R[10100];
bool bf[10100];
int n,m,i,x,y,e,ans;
bool ok;


int bag(int x)
{
    int i;
    if (bf[x]==true) return 0;
    bf[x]=true;

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

    for (i=0;i<a[x].size();i++)
        if (bag(a[x][i]))
        {
            R[a[x][i]]=x;
            L[x]=a[x][i];
            return 1;
        }
    return 0;
}


int main()
{
	f=fopen("cuplaj.in","r");
	g=fopen("cuplaj.out","w");

    fscanf(f,"%d%d%d",&n,&m,&e);
    for (i=1;i<=e;i++)
    {
        fscanf(f,"%d%d",&x,&y);
        a[x].push_back(y);
    }


    for (ok=true;ok;)
    {
        for (i=1;i<=n;i++)
            bf[i]=false;

        for (i=1,ok=false;i<=n;i++)
            if (!L[i])
                ok|=bag(i);
    }


    for (i=1;i<=n;i++)
        if (L[i]>0)
            ans++;
    fprintf(g,"%d\n",ans);

    for (i=1;i<=n;i++)
        if (L[i]>0)
            fprintf(g,"%d %d\n",i,L[i]);

	return 0;
}