Cod sursa(job #753906)

Utilizator test1Trying Here test1 Data 30 mai 2012 19:07:00
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.1 kb
#include <cstdio>
#include <algorithm>
#include <vector>
#include <cstring>
#define N_MAX 10010
using namespace std;

vector<int>g[N_MAX];
int st[N_MAX],dr[N_MAX],n,m,nr;
bool viz[N_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]))
        {
            dr[x] = y;
            st[y] = x;
            return 1;
        }
    }
    return 0;
}


void hopcroft_karp(){
    int nr_muchii = 0;
    bool ok = 1;
    while(ok)
    {
        ok = 0;
        memset(viz,0,sizeof(viz));
        for(int i=1;i<=n;i++)
        if(dr[i] == 0 && dfs(i))
        {
            ok = 1;
            nr_muchii++;
        }
    }
    printf("%d\n",nr_muchii);
    for(int i=1;i<=n;i++)
    if(dr[i]) printf("%d %d\n",i,dr[i]);
}

int main(){
    int x,y;
    freopen("cuplaj.in","r",stdin);
    freopen("cuplaj.out","w",stdout);
        scanf("%d %d %d",&n,&m,&nr);
        for(int i=0;i<nr;i++)
        {
            scanf("%d %d",&x,&y);
            g[x].push_back(y);
        }
    hopcroft_karp();
    return 0;
}