Cod sursa(job #756602)

Utilizator test1Trying Here test1 Data 9 iunie 2012 22:55:00
Problema Cuplaj maxim in graf bipartit Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.03 kb
#include <cstdio>
#include <cstring>
#include <vector>
#define MAX 10010
using namespace std;

vector<int>u[MAX];
int st[MAX], dr[MAX];
int n,m, nr_c;
bool viz[MAX];

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

void cuplaj_maxim(){
    bool ok = 1;
    nr_c = 1;
    while( ok )
    {
        ok = 0;
        nr_c++;
        memset( viz,0,sizeof(viz) );
        for(int i=1;i<=n;i++)
        if( dr[i] == 0 && dfs(i) ) ok = 1;
    }
}

int main(){
    int nr, x, y;
    freopen("cuplaj.in","r",stdin);
    freopen("cuplaj.out","w",stdout);
        scanf("%d %d %d",&n,&m,&nr);
        while(nr--)
        {
            scanf("%d %d",&x,&y);
            u[x].push_back(y);
        }
    cuplaj_maxim();

    printf("%d\n",nr_c);
    for(int i=1;i<=n;i++)
        if( dr[i] ) printf("%d %d\n",i,dr[i]);
    return 0;
}