Cod sursa(job #1249424)

Utilizator DenisacheDenis Ehorovici Denisache Data 26 octombrie 2014 23:13:30
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.88 kb
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <stack>
#include <queue>
#include <list>
//#include <windows.h>
//#include <conio.h>
#include <cstdlib>
#include <time.h>
#include <limits.h>
#include <string>
#include <math.h>
#include <iomanip>
using namespace std;
#define forA(V,it) for (typeof(V.begin()) it=V.begin();it!=V.end();it++)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define ull unsigned ll
#define MOD 1000000007
#define INF (1<<31)-1
#define MINF -(1<<31)
#define vi vector <int>
#define vll vector <ll>
#define pii pair <int,int>
#define pll pair <ll,ll>
#define newl printf("\n")
#define DIM 10000000
vi G[10005];
int l[10005],r[10005],con,i,N,M,E;
bool used[10005];
bool pairup(int nod)
{
    if (used[nod]) return false;
    used[nod]=true;
    for (vi::iterator it=G[nod].begin();it!=G[nod].end();it++)
    {
        if (!r[*it])
        {
            l[nod]=*it;
            r[*it]=nod;
            return true;
        }
    }
    for (vi::iterator it=G[nod].begin();it!=G[nod].end();it++)
    {
        if (pairup(r[*it]))
        {
            l[nod]=*it;
            r[*it]=nod;
            return true;
        }
    }
    return false;
}
int main()
{
    freopen("cuplaj.in","r",stdin);
    freopen("cuplaj.out","w",stdout);
    scanf("%d %d %d",&N,&M,&E);
    for (i=1;i<=E;i++)
    {
        int x,y;
        scanf("%d %d",&x,&y);
        G[x].pb(y);
    }
    bool ok;
    do
    {
        ok=false;
        memset(used,false,sizeof(used));
        for (i=1;i<=N;i++)
            if (!l[i]) ok|=pairup(i);
    } while (ok);
    for (i=1;i<=N;i++)
        if (l[i]>0) con++;
    printf("%d\n",con);
    for (i=1;i<=N;i++)
        if (l[i]>0) printf("%d %d\n",i,l[i]);
    return 0;
}