Cod sursa(job #1244761)

Utilizator FayedStratulat Alexandru Fayed Data 18 octombrie 2014 05:29:51
Problema Cuplaj maxim in graf bipartit Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.52 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];
char cc[4];
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);

    int ii;
    for(register int i=1;i<=R;++i){

        ii = 0;
        for(register int k = 0; k < 3; ++k)
        scanf("%s",&cc[k]);

        x = cc[ii++] - '0';
        ++ii;
        y = cc[ii] - '0';

        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(!dr[*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;
}