Cod sursa(job #763519)

Utilizator test666013Testez test666013 Data 2 iulie 2012 15:25:04
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.09 kb
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
#define MAX 10001

vector<int>g[MAX];

int n,m,st[MAX],dr[MAX];
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 nr = 0;
    bool ok = 1;
    while(ok)
    {
        ok = 0;
        memset(was,0,sizeof(was));
        for(int i=1;i<=n;i++)
        if(dr[i] == 0 && dfs(i))
        {
            nr++; // am mai gasit un cuplaj
            ok = 1; // vom mai itera un pas
        }
    }
    printf("%d\n",nr);
    for(int i=1;i<=n;i++)
    if(dr[i]) printf("%d %d\n",i,dr[i]);
}

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);
            g[x].push_back(y);
        }
    cuplaj();
    return 0;
}