Cod sursa(job #2233576)

Utilizator alextodoranTodoran Alexandru Raul alextodoran Data 23 august 2018 17:19:03
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.2 kb
#include <bits/stdc++.h>

#define NM 10005

using namespace std;

vector <int> G[NM];

int n, m, e;
int L[NM], R[NM];
bool U[NM];
int nr;

bool pairup(int node)
{
    if(U[node])
        return 0;
    U[node] = 1;
    for(auto i : G[node])
        if(!R[i])
        {
            L[node] = i;
            R[i] = node;
            return 1;
        }
    for(auto i : G[node])
        if(pairup(R[i]))
        {
            L[node] = i;
            R[i] = node;
            return 1;
        }
    return 0;
}

int main()
{
    ifstream fin ("cuplaj.in");
    ofstream fout ("cuplaj.out");
    fin >> n >> m >> e;
    for(int i = 1; i <= e; i++)
    {
        int a, b;
        fin >> a >> b;
        G[a].push_back(b);
    }
    int ok = 1;
    while(ok)
    {
        ok = 0;
        for(int i = 1; i <= n; i++)
            U[i] = 0;
        for(int i = 1; i <= n; i++)
            if(!L[i])
                if(pairup(i))
                    ok = 1;
    }
    for(int i = 1; i <= n; i++)
        if(L[i])
           nr++;
    fout << nr << "\n";
    for(int i = 1; i <= n; i++)
        if(L[i])
            fout << i << " " << L[i] << "\n";
    return 0;
}