Cod sursa(job #2192116)

Utilizator Narvik13Razvan Roatis Narvik13 Data 4 aprilie 2018 19:29:22
Problema Cuplaj maxim in graf bipartit Scor 20
Compilator cpp Status done
Runda Arhiva educationala Marime 1.33 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <bitset>
#define VMAX 100002

using namespace std;

ifstream f("cuplaj.in");
ofstream o("cuplaj.out");

int n,m,e,total;
int dr[VMAX],st[VMAX];
vector <int> g[VMAX];
bitset <VMAX> viz;

void citire()
{
    f >> n >> m >> e;
    int x,y;
    for(int i = 1; i <= e; ++i)
    {
        f >> x >> y;
        g[x].push_back(y);
    }
}

bool pair_up(int nod)
{
    if(nod && viz[nod])
        return false;
    viz[nod] = true;
    for(auto i: g[nod])
        if(st[i] == 0)
        {
            dr[nod] = i;
            st[i] = nod;
            viz[nod] = false;
            return true;
        }
        else if(pair_up(st[i]))
        {
            dr[nod] = i;
            st[i] = nod;
            viz[nod] = false;
            return true;
        }
    return false;
}

void cuplare()
{
    bool ok = true;
    while(ok)
    {
        ok = false;
        for(int i = 1; i <= n; ++i)
            if(pair_up(i))
            {
                ok = true;
                ++ total;
            }
    }
}

void output()
{
    o << total << '\n';
    for(int i = 1; i <= n; ++i)
        if(dr[i])
            o << i << ' ' << dr[i] << '\n';
}

int main()
{
    citire();
    cuplare();
    output();
    return 0;
}