Cod sursa(job #1438376)

Utilizator eneandradaEne Oana-Andrada eneandrada Data 19 mai 2015 20:06:35
Problema Cuplaj maxim in graf bipartit Scor 40
Compilator cpp Status done
Runda Arhiva educationala Marime 1.14 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <string.h>

using namespace std;

#define MAXN 10005

ifstream f("cuplaj.in");
ofstream g("cuplaj.out");

vector <int> G[MAXN];
int Left[MAXN], Right[MAXN];
bool viz[MAXN];

int pairup(int nod)
{
    if(viz[nod])
        return 0;
    viz[nod] = true;
    for(int i = 0; i < G[nod].size(); i++)
    {
        if(Right[G[nod][i]] == 0 || pairup(Right[G[nod][i]]))
        {
            Left[nod] = G[nod][i];
            Right[G[nod][i]] = nod;
            return 1;
        }
    }
    return 0;
}

int main()
{
    int x, y, N, M, E, c = 0;

    f >> N >> M >> E;
    for(int i = 1; i <= E; i++)
    {
        f >> x >> y;
        G[x].push_back(y);
    }
    for(int i = 1; i <= N ; ++i)
    {
        if(Left[i] == 0)
        {
            if(!pairup(i))
                {
                    memset(viz, 0, sizeof(bool)*(N+1));

                }
            else
                c++;
        }
    }
    g << c << endl;
    for(int i = 1; i <= N; ++i)
        if(Left[i] > 0)
            g<<i<<" "<<Left[i]<<endl;
    return 0;
}