Cod sursa(job #951151)

Utilizator razvan9310FMI - Razvan Damachi razvan9310 Data 19 mai 2013 16:31:04
Problema Cuplaj maxim in graf bipartit Scor 20
Compilator cpp Status done
Runda Arhiva educationala Marime 3.01 kb
#include <fstream>
#include <vector>
#include <cstring>
using namespace std;

class Bipartite_Graph
{
    int V1, V2;
    vector<int> *G;
    int *M1, *M2;
    bool *used, *matched;

    bool match(const int &vertex)
    {
        if (used[vertex] == 1)
            return 0;
        used[vertex] = 1;
        int i, size = G[vertex].size();
        for (i=0; i<size; ++i)
        {
            int adjancted = G[vertex][i];
            if (M2[adjancted] == -1)
            {
                M1[vertex] = adjancted;
                M2[adjancted] = vertex;
                return 1;
            }
        }
        for (i=0; i<size; ++i)
        {
            int adjancted = G[vertex][i];
            if (match(M2[adjancted]) == 1)
            {
                M1[vertex] = adjancted;
                M2[adjancted] = vertex;
                return 1;
            }
        }
        return 0;
    }

    public:
        Bipartite_Graph(const int &V1_source, const int &V2_source)
        {
            V1 = V1_source;
            V2 = V2_source;

            G = new vector<int>[V1+1];
            M1 = new int[V1+1];
            for (int i=1; i<=V1; ++i)
                M1[i] = -1;
            M2 = new int[V2+1];
            for (int i=1; i<=V2; ++i)
                M2[i] = -1;

            used = new bool[V1];
            memset(used, 0, sizeof(used));
            matched = new bool[V1];
            memset(matched, 0, sizeof(matched));
        }
        ~Bipartite_Graph()
        {
            delete[] G;
            delete[] M1;
            delete[] M2;
            delete[] used;
            delete[] matched;
        }

        void addEdge(const int &v1, const int &v2)
        {
            G[v1].push_back(v2);
        }

        void setMaximumMatch()
        {
            bool done_something = 1;
            do
            {
                memset(used, 0, sizeof(used));
                done_something = 0;
                for (int i=1; i<=V1; ++i)
                    if (matched[i] == 0)
                        if (match(i) == 1)
                        {
                            matched[i] = 1;
                            done_something = 1;
                        }
            }
            while (done_something == 1);
        }

        void printMaximumMatch(ofstream &out) const
        {
            int i, count = 0;
            for (i=1; i<=V1; ++i)
                count += (M1[i] != -1);
            out<<count<<"\n";
            for (i=1; i<=V1; ++i)
                if (M1[i] != -1) out<<i<<" "<<M1[i]<<"\n";
            out.close();
        }
};

int main()
{
    int V1, V2, M;
    ifstream in("cuplaj.in");
    in>>V1>>V2>>M;
    Bipartite_Graph G(V1, V2);
    for (int i=0; i<M; ++i)
    {
        int from, to;
        in>>from>>to;
        G.addEdge(from, to);
    }
    in.close();

    ofstream out("cuplaj.out");
    G.setMaximumMatch();
    G.printMaximumMatch(out);
    out.close();

    return 0;
}