Cod sursa(job #2937631)

Utilizator bogdan.schiopBogdan Schiop bogdan.schiop Data 10 noiembrie 2022 19:08:27
Problema Cuplaj maxim in graf bipartit Scor 50
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.23 kb
#include <iostream>
#include <fstream>

using namespace std;

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

int a[10001][10001];
int n, m, e;
int a1, a2;
int viz[20002];
int st[10001];
int dr[10001];

void citire()
{
    fin >> n >> m >> e;
    for(int i = 1; i <= e; i++)
    {
        fin >> a1 >> a2;
        a[a1][a2] = 1;
    }
}

int rez(int p)
{
    if(viz[p] == 1) return 0;
    viz[p] = 1;
    for(int i = 1; i <= n; i++)
    {
        if(a[p][i] == 1 && !dr[i])
        {
            dr[i] = p;
            st[p] = i;
            return 1;
        }
        if(a[p][i] == 1 && rez(dr[i]) == 1)
        {
            st[p] = i;
            dr[i] = p;
            return 1;
        }
    }
    return 0;
}

int main()
{
    citire();
    int ok = 1;
    int c = 0;
    while(ok == 1)
    {
        ok = 0;
        for(int i = 1; i <= n; i++)
            viz[i] = 0;
        for(int i = 1; i <= n; i++)
            if(st[i] == 0 && rez(i) == 1)
            ok = 1;
    }
    for(int i = 1; i <= n; i++)
    {
        if(st[i]) c++;
    }
    fout << c << endl;
    for(int i = 1; i <= n; i++)
    {
        if(st[i]) fout << i << ' ' << st[i] << endl;
    }
    return 0;
}