Cod sursa(job #1985013)

Utilizator raulmuresanRaul Muresan raulmuresan Data 26 mai 2017 17:55:54
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.54 kb
//Cuplaj maxim in graf bipertit
#include <bits/stdc++.h>

using namespace std;

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

const int MAX = 10005;
int n, m, e, l[MAX], r[MAX], sol, viz[MAX];
vector<int> v[MAX];

int pair_up(int node){
    if (viz[node])
        return 0;
    else
    {
        viz[node] = 1;

        for (auto neighbour: v[node])
        {
            if (r[neighbour] == 0){
                //if we find a not visited neighbour we  connect the current node with
                //this neighbour
                l[node] = neighbour;
                r[neighbour] = node;
                return 1;
            }else if (pair_up(r[neighbour]) == 1){
                //if the neighbour is visited, we try to find a new connection
                //for its current cennection in order to be able to connect the current node with this neighbour
                l[node] = neighbour;
                r[neighbour] = node;
                return 1;
            }
        }


    return 0;
    }

}

int main(){
    fin >> n >> m >> e;
    for (int a, b; e; e--){
        fin >> a >> b;
        v[a].push_back(b);
    }

    for (int ok = 1; ok;){
        ok = 0;
        memset(viz, 0, sizeof(viz));
        for (int i = 1; i <= n; i++)
            if (!l[i] && pair_up(i) == 1){
                ok = 1;
                sol++;
            }
    }

    fout << sol << '\n';

    for (int i = 1; i <= n; i++)
        if(l[i])
            fout << i << ' ' << l[i] << '\n';

    return 0;
}