Cod sursa(job #1245823)

Utilizator FayedStratulat Alexandru Fayed Data 20 octombrie 2014 03:17:26
Problema Cuplaj maxim in graf bipartit Scor 80
Compilator cpp Status done
Runda Arhiva educationala Marime 1.68 kb
#include <cstdio>
#include <cstring>
#include <fstream>
#include <cstdlib>
#include <vector>
#define NMAX 10001
using namespace std;

int n,m,R;
int cuplajMax;
int st[NMAX];
int dr[NMAX];
char cc[20];
bool visited[NMAX];
vector < vector < int > > Graph;
ifstream f("cuplaj.in");
ofstream g("cuplaj.out");

inline void read(){

    int x = 0,y = 0;
    f >> n >> m >> R;
    Graph.resize(2*n+1);

     f.getline(cc,20);
    for(register int i=1;i<=R;++i){

        int ii = 0;
        x = y = 0;
        f.getline(cc,20);
        while(cc[ii] >= '0' && cc[ii] <= '9')
            x = x*10 + (cc[ii++] - '0');

        ++ii;
        while(cc[ii] >= '0' && cc[ii] <= '9')
            y = y*10 + (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();

    g << cuplajMax << '\n';
    for(register int node = 1; node <= n; ++node)
        if(st[node])
            g << node << " " << st[node] << '\n';

f.close();
g.close();

return 0;
}