Cod sursa(job #1438764)

Utilizator eneandradaEne Oana-Andrada eneandrada Data 20 mai 2015 19:36:43
Problema Cuplaj maxim in graf bipartit Scor 80
Compilator cpp Status done
Runda Arhiva educationala Marime 1.32 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)
        {
            Left[nod] = G[nod][i];
            Right[G[nod][i]] = nod;
            return 1;
        }
    for(int i = 0; i < G[nod].size(); i++)
        if(pairup(Right[G[nod][i]]) == 1)
        {
            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) == 0)
            {
                memset(viz, false, sizeof(bool)*(N+1));
                c += pairup(i);
            }
            else
                c++;
        }
    }
    g << c << endl;
    for(int i = 1; i <= N; ++i)
        if(Left[i] > 0)
            g<<i<<" "<<Left[i]<<endl;
    return 0;
}