Cod sursa(job #1244757)

Utilizator FayedStratulat Alexandru Fayed Data 18 octombrie 2014 05:12:28
Problema Cuplaj maxim in graf bipartit Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.36 kb
#include <cstdio>
#include <cstring>
#include <vector>
#define NMAX 10001
using namespace std;

int n,m,R;
int cuplajMax;
int st[NMAX];
int dr[NMAX];
bool visited[NMAX];
vector < vector < int > > Graph;

inline void read(){

    int x,y;
    freopen("cuplaj.in","r",stdin);
    freopen("cuplaj.out","w",stdout);
    scanf("%d%d%d",&n,&m,&R);
    Graph.resize(2*n+1);

    for(register int i=1;i<=R;++i){
        scanf("%d%d",&x,&y);
        Graph[x].push_back(y);
    }
}

bool Hopcroft_Karp(int node){

    if(visited[node])
        return 0;

    visited[node] = 1;
    for(vector < int > :: iterator it = Graph[node].begin(); it!= Graph[node].end(); ++it)
    if(!st[*it] || Hopcroft_Karp(dr[*it])){
        st[node] = *it;
        dr[*it] = node;
        return 1;
    }
return 0;
}

inline void solve(){

    bool ok = 1;
    while(ok){

        ok = 0;
        for(register int node = 1; node <= n; ++ node)
            if(!st[node])
                if(Hopcroft_Karp(node)){
                    ok = 1;
                    ++cuplajMax;
                    memset(visited,0,sizeof(visited));
              }
    }
}

int main(){

    read();
    solve();

    printf("%d\n",cuplajMax);
    for(register int node = 1; node <= n; ++node)
        if(st[node])
            printf("%d %d\n",node,st[node]);

return 0;
}