Cod sursa(job #767355)

Utilizator test_666013Testez test_666013 Data 13 iulie 2012 13:05:47
Problema Cuplaj maxim in graf bipartit Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.05 kb
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
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;
        }
    }
}

void cuplaj(){
    int nr = 0;
    bool next = 1;
    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);
        scanf("%d %d %d",&n,&m,&e);
        while(e--)
        {
            scanf("%d %d",&x,&y);
            g[x].push_back(y);
        }

    cuplaj();
    return 0;
}