Cod sursa(job #781336)

Utilizator my666013Test Here my666013 Data 24 august 2012 10:58:41
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.27 kb
#include <cstdio>
#include <cstring>
#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; // nu putem cupla aici
    was[x] = 1;

    for(int i=0;i<g[x].size();i++)
    {
        y = g[x][i];
        if( st[y] == 0 || dfs(st[y]) ) // daca nu-i cuplat sau
        {                           // ii putem gasi o alta pereche
            st[y] = x;
            dr[x] = y;
            return 1; // am cuplat
        }
    }
    return 0;

}

void cuplaj(){
    int nr = 0, step = 1;
    while(step)
    {
        step = 0;
        memset(was,0,sizeof(was));
        for(int i=1;i<=n;i++)
        if( !dr[i] && dfs(i) ) // daca nu are un corespodent
        {                      // si putem gasi unul
            nr++; // am mai cuplat o pereche
            step = 1; // mai repetam odata ciclul
        }
    }

    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);

        scanf("%d %d %d",&n,&m,&e);

        while(e--)
        {
            scanf("%d %d",&x,&y);
            g[x].push_back(y);
        }

    cuplaj();

    return 0;
}