Pagini recente » Cod sursa (job #2820522) | Cod sursa (job #315583) | Cod sursa (job #1705666) | Cod sursa (job #763362) | Cod sursa (job #2417623)
#include <bits/stdc++.h>
#define RIGHT 0
#define LEFT 1
class BipartiteMatching {
public:
BipartiteMatching(std::vector<std::vector <int>>& graph) : count(0) {
for (int i=0; i<2; ++i) pair[i].resize(graph.size(), -1);
seen.resize(graph.size(), 0);
bool bFlag = 1;
while (bFlag) {
bFlag = 0;
for (int i=0; i<graph.size(); ++i)
seen[i] = 0;
for (int i=0; i<graph.size(); ++i)
if (pair[RIGHT][i] == -1 && pairUp(i, graph))
bFlag = 1, ++ count;
}
}
int size() const {return count;}
int operator[] (int idx) {return pair[RIGHT][idx];}
protected:
int count;
std::vector <int> pair[2];
std::vector <bool> seen;
bool pairUp(int vertex, std::vector<std::vector <int>>& graph) {
if (seen[vertex]) return false;
seen[vertex] = 1;
for (auto adj:graph[vertex])
if (pair[LEFT][adj] == -1 || pairUp(pair[LEFT][adj], graph))
{pair[LEFT][adj] = vertex; pair[RIGHT][vertex] = adj; return true;}
return false;
}
};
int N, M, E;
std::vector<std::vector <int>> graph;
inline void addEdge(int x, int y) {
graph[x].push_back(y);
}
std::ifstream input ("cuplaj.in");
std::ofstream output("cuplaj.out");
void readInput() {
input >> N >> M >> E;
graph.resize(std::max(N, M), std::vector <int> ());
for (int i=0, x, y; i<E; ++i)
input >> x >> y, addEdge(--x, --y);
}
void solveInput() {
BipartiteMatching match(graph);
output << match.size() << '\n';
for (int i=0; i<N; ++i)
if (match[i] != -1)
output << i+1 << ' ' << match[i]+1 << '\n';
}
int main()
{
readInput();
solveInput();
return 0;
}