Cod sursa(job #2246298)

Utilizator circeanubogdanCirceanu Bogdan circeanubogdan Data 26 septembrie 2018 21:53:08
Problema Cuplaj maxim in graf bipartit Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.7 kb
#include <fstream>
#include <vector>
#define DIM 10002

using namespace std;

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

int leftNum, rightNum, edgesNum, leftNode, rightNode;
int toLeft[DIM], toRight[DIM];

vector<int> graph[DIM];
vector<pair<int, int> > solution;

bool verify = true;

bitset<DIM> visited;

bool makeMatch(int currentNode, int nextNode){
    toRight[currentNode] = nextNode;
    toLeft[nextNode] = currentNode;
    return true;
}

bool match(int currentNode){
    if(visited[currentNode])
        return false;
    visited[currentNode] = true;
    
    for(auto nextNode : graph[currentNode]){
        if(!toLeft[nextNode]){
            return makeMatch(currentNode, nextNode);
        }
    }
    
    for(auto nextNode : graph[currentNode]){
        if(match(toLeft[nextNode])){
            return makeMatch(currentNode, nextNode);
        }
    }
    
    return false;
}

int main(int argc, const char * argv[]) {
    
    in>>leftNum>>rightNum>>edgesNum;
    
    for(int edgesCnt = 1; edgesCnt <= edgesNum; ++ edgesCnt){
        in>>leftNode>>rightNode;
        graph[leftNode].push_back(rightNode);
    }
    
    while(verify){
        verify = false;
        visited.reset();
        for(int nodesCnt = 1; nodesCnt <= leftNum; ++ nodesCnt){
            if(!toRight[nodesCnt]){
                verify |= match(nodesCnt);
            }
        }
    }
    
    for(int nodesCnt = 1; nodesCnt <= leftNum; ++ nodesCnt){
        if(toRight[nodesCnt]){
            solution.push_back(make_pair(nodesCnt, toRight[nodesCnt]));
        }
    }
    
    out<<solution.size()<<'\n';
    for(auto currentEdge : solution){
        out<<currentEdge.first<<" "<<currentEdge.second<<'\n';
    }
    
    return 0;
}