Cod sursa(job #2173124)

Utilizator andreigeorge08Sandu Ciorba andreigeorge08 Data 15 martie 2018 20:47:05
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.2 kb
#include <bits/stdc++.h>

using namespace std;

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

int st[10005], dr[10005], n, m, e;
bool viz[10005];
vector <int> L[10005];

inline bool Cupleaza(int x)
{
    if(viz[x] == 1) return 0;
    viz[x] = 1;

    for(auto i : L[x])
        if(dr[i] == 0)
        {
            st[x] = i;
            dr[i] = x;
            return 1;
        }

    for(auto i : L[x])
        if(Cupleaza(dr[i]))
        {
            st[x] = i;
            dr[i] = x;
            return 1;
        }

    return 0;
}

void Citire()
{
    int x, y;

    fin >> n >> m >> e;
    for(int i = 1; i <= e; i++)
    {
        fin >> x >> y;
        L[x].push_back(y);
    }

    bool gata = 0;
    int cuplaje = 0;

    while(!gata)
    {
        gata = 1;

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

        for(int i = 1; i <= n; i++)
            if(st[i] == 0 && Cupleaza(i))
                cuplaje++,
                gata = 0;
    }

    fout << cuplaje << "\n";

    for(int i = 1; i <= n; i++)
        if(st[i] != 0)
        fout << i << " " << st[i] << "\n";
}
int main()
{
    Citire();
    return 0;
}