Cod sursa(job #785448)

Utilizator round1First Round round1 Data 9 septembrie 2012 00:48:22
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.21 kb
#include <cstdio>
#include <vector>
#include <cstring>
#include <algorithm>
using namespace std;
#define Max 10001

vector<int>g[Max];
int st[Max],dr[Max],n,m;
bool was[Max];

bool dfs(int x)
{
    int y;
    if( was[x] )return 0;
    was[x] = 1;
    for(int i=0;i<g[x].size();i++)
    {
        y = g[x][i];
        if( st[y] == 0 || dfs(st[y]) )
        {
            st[y] = x;
            dr[x] = y;
            return 1;
        }
    }
    return 0;
}

void cuplaj(){
    int next = 1;
    int nr = 0;
    while( next )
    {
        next = 0;
        memset(was,0,sizeof(was));
        for(int i=1;i<=n;i++)
        if( dr[i] == 0 && dfs(i) )
        {
            nr ++;
            next = 1;
        }
    }
        printf("%d\n",nr);
        for(int i=1;i<=n;i++)
        if( dr[i] ) printf("%d %d\n",i,dr[i]);
}

int main()
{
    int e,x,y;
    freopen("cuplaj.in","r",stdin);
    freopen("cuplaj.out","w",stdout);
    //memset(st,0,sizeof(st));
   // memset(dr,0,sizeof(dr));

        scanf("%d %d %d",&n,&m,&e);
        for(int i=1;i<=e;i++)
        {
            scanf("%d %d",&x,&y);
            g[x].push_back(y);
        }
    cuplaj();

    return 0;
}