Cod sursa(job #2181951)

Utilizator remus88Neatu Remus Mihai remus88 Data 21 martie 2018 22:57:46
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.31 kb
#include <fstream>
#include <vector>
#include <bitset>
#define Nmax 10009

using namespace std;
ifstream f("cuplaj.in");
ofstream g("cuplaj.out");

int n,m,edges,x,y,st[Nmax],dr[Nmax];
vector <int> G[Nmax];
bitset <Nmax> viz;

void ReadInput() {

    f>>n>>m>>edges;

    for (int i=1; i<=edges; ++i) {

        f>>x>>y;
        G[x].push_back(y);
    }
}

int cupleaza (int node) {

    if (viz[node]) return 0;

    viz[node]=1;

    for (auto x: G[node])
        if (!st[x]) {

            st[x]=node;
            dr[node]=x;
            return 1;
        }

    for (auto x: G[node])
        if (cupleaza(st[x])) {

            st[x]=node;
            dr[node]=x;
            return 1;
        }

    return 0;
}

void Solve() {

    int sol=0;

    bool verif=false;

    while (!verif) {

        for (int i=1; i<=n; ++i)
            viz[i] = 0;

        verif=true;

        for (int i=1; i<=n; ++i) {

            if (dr[i]==0 && cupleaza(i) ) {

                ++sol;
                verif=false;


            }
        }
    }

    g<<sol<<'\n';
    for (int i=1; i<=n; ++i)
        if (dr[i]) {

            g<<i<<' '<<dr[i]<<'\n';
        }

}

int main() {

    ReadInput();
    Solve();

    f.close(); g.close();
    return 0;
}