Cod sursa(job #1690223)

Utilizator tudormaximTudor Maxim tudormaxim Data 14 aprilie 2016 21:31:20
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.25 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <bitset>
using namespace std;

ifstream fin ("cuplaj.in");
ofstream fout ("cuplaj.out");

const int nmax = 1e4+5;
vector <int> g[nmax];
bitset <nmax> viz;
int n, m, kid[nmax], dady[nmax];

bool cuplaj(int dad) {
    if(viz[dad]==true) return false;
    viz[dad]=true;
    for(auto son : g[dad]) {
        if(dady[son]==0) {
            dady[son]=dad;
            kid[dad]=son;
            return true;
        }
        if(cuplaj(dady[son])) {
            dady[son]=dad;
            kid[dad]=son;
            return true;
        }
    }
    return false;
}

void cuplaj_maxim() {
    int i, cup=0;
    bool ok=true;
    while(ok) {
        ok=false;
        viz=0;
        for(i=1; i<=n; i++)
            if(!kid[i] && cuplaj(i)) ok=true;
    }
    for(i=1; i<=n; i++)
        if(kid[i]) cup++;
    fout << cup << "\n";
    for(i=1; i<=n; i++)
        if(kid[i]) fout << i << " " << kid[i] << "\n";
}

int main() {
    ios_base::sync_with_stdio(false);
    int e, i, x, y;
    fin >> n >> m >> e;
    for(i=1; i<=e; i++) {
        fin >> x >> y;
        g[x].push_back(y);
    }
    cuplaj_maxim();
    fin.close();
    fout.close();
    return 0;
}