Cod sursa(job #1404435)

Utilizator hopingsteamMatraguna Mihai-Alexandru hopingsteam Data 28 martie 2015 10:40:00
Problema Cuplaj maxim in graf bipartit Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.37 kb
#include    <iostream>
#include    <cstring>
#include    <fstream>
#include    <vector>

using namespace std;

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

const int LIM = 10005;
int N, M, E;
int Left[LIM], Right[LIM], Used[LIM];

vector < int > Edges[LIM];

int pairup(int X)
{
    if(Used[X])
        return 0;

    Used[X] = 1;

    for(unsigned int i = 0; i < Edges[X].size(); i++)
    {
        if(!Right[ Edges[X][i] ])
        {
            Left[ X ]               =   Edges[X][i];
            Right[ Edges[X][i] ]    =   X;
            return 1;
        }
    }

    for(unsigned int i = 0; i < Edges[X].size(); i++)
    {
        if(pairup( Right[ Edges[X][i] ] ))
        {
            Left[ X ]       =    Edges[X][i];
            Right[ Edges[X][i] ] = X;
            return 1;
        }
    }

    return 0;
}

void Read()
{
    fin >> N >> M >> E;
    for(int i = 1; i <= E; i++)
    {
        int x, y;
        fin >> x >> y;
        Edges[x].push_back(y);
    }

    for(int change = 1; change; /* */)
    {
        change = 0;
        memset(Used, 0, sizeof(Used));
        for(int i = 1; i <= N; i++)
        {
            if(!Left[i])
            {
                if(pairup(i))
                    change = pairup(i);
            }
        }
    }
}

int main()
{
    Read();
    return 0;
}