Pagini recente » Cod sursa (job #1134457) | Cod sursa (job #1601934) | Cod sursa (job #1824573) | Cod sursa (job #1525397) | Cod sursa (job #2246298)
#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;
}