Cod sursa(job #1449249)

Utilizator alexb97Alexandru Buhai alexb97 Data 9 iunie 2015 09:05:27
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.32 kb
#include <fstream>
#include <vector>
#include <cstring>
using namespace std;

ifstream is("cuplaj.in");
ofstream os("cuplaj.out");

vector<vector<int>> g;
int n, m, k;
int v[10002];
vector<int> L, R;

void Read();
bool Cupleaza(int x);

int main()
{
    Read();
    int cntcup = 0;
    bool ok = false;
    do
    {
        memset(v, false, sizeof(v));
        ok = false;
        for(int i = 1; i <= n; ++i)
        {
            if(!L[i] && Cupleaza(i))
            {
                cntcup++;
                ok = true;
            }
        }
    }while(ok);

    os << cntcup  << '\n';
    for(int i = 1; i <= n; ++i)
    {
        if(L[i])
            os << i << ' ' << L[i] << '\n';
    }

    is.close();
    os.close();
    return 0;
}


void Read()
{
    is >> n >> m >> k;
    g = vector<vector<int>>(n+m+1);
    L = vector<int>(n+1);
    R = vector<int>(m+1);
    int x, y;
    for(int i = 0; i < k; ++ i)
    {
        is >> x >> y;
        g[x].push_back(y);
    }
}

bool Cupleaza(int x)
{
    if(v[x]) return false;
    v[x] = 1;
    for( vector<int>::iterator i = g[x].begin(); i != g[x].end(); ++i)
    {
        if(!R[*i] || Cupleaza(R[*i]))
        {
            L[x] = *i;
            R[*i] = x;
            return true;
        }
    }
    return false;
}