Cod sursa(job #759201)

Utilizator test13test13 test13 Data 17 iunie 2012 09:37:57
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.99 kb
#include <cstdio>
#include <vector>
#include <cstring>
using namespace std;
#define MAX 10001

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

bool dfs(int x){
    int y;
    if(viz[x])return 0;
    viz[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,ok=1;
    while(ok)
    {
        ok=0;
        memset(viz,0,sizeof(viz));
        for(int i=1;i<=n;i++)
        if(dr[i]==0 && dfs(i))
        {
            nr++;
            ok=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 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;
}