Pagini recente » Cod sursa (job #2836723) | Cod sursa (job #2366496) | Cod sursa (job #42696) | Cod sursa (job #1532037) | Cod sursa (job #2140076)
#include<fstream>
#include<bitset>
#include<list>
using namespace std;
ifstream fin("cuplaj.in");
ofstream fout("cuplaj.out");
const int NMAX = 1e4 + 5;
bool used[NMAX];
list<int>graph[NMAX];
int leftMatch[NMAX], rightMatch[NMAX];
int leftNodes, rightNodes, edgesCount;
inline void readData(){
fin >> leftNodes >> rightNodes >> edgesCount;
int from, to;
while(edgesCount--){
fin >> from >> to;
graph[from].push_back(to);
}
}
bool match(int node){
if(used[node])
return false;
used[node] = true;
for(const auto &nextNode:graph[node])
if(!leftMatch[nextNode] or match(leftMatch[nextNode])){
rightMatch[node] = nextNode;
leftMatch[nextNode] = node;
return true;
}
return false;
}
inline void getMaxMatching(){
bool possibleMatching = true;
int node;
while(possibleMatching){
possibleMatching = false;
fill(used + 1, used + 1 + leftNodes, false);
for(node = 1; node <= leftNodes; ++node)
if(!rightMatch[node])
if(match(node))
possibleMatching = true;
}
}
inline void printMaxMatching(){
int node, matching = 0;
for(node = 1; node <= leftNodes; ++node)
if(rightMatch[node])
++matching;
fout << matching << '\n';
for(node = 1; node <= leftNodes; ++node)
if(rightMatch[node])
fout << node << ' ' << rightMatch[node] << '\n';
}
int main(){
readData();
getMaxMatching();
printMaxMatching();
}