Pagini recente » Cod sursa (job #2036126) | Cod sursa (job #1110051) | Cod sursa (job #488904) | Cod sursa (job #2987045) | Cod sursa (job #1896351)
#include <fstream>
#include <vector>
using namespace std;
using Graph = vector<vector<int>>;
bool FindPair(const Graph &g, vector<bool> &vis, vector<int> &so1, vector<int> &so2, int node)
{
if (vis[node]) {
return false;
}
vis[node] = true;
for (int next : g[node]) {
if (so2[next] == -1) {
so1[node] = next;
so2[next] = node;
return true;
}
}
for (int next : g[node]) {
if (FindPair(g, vis, so1, so2, so2[next])) {
so1[node] = next;
so2[next] = node;
return true;
}
}
return false;
}
vector<pair<int, int>> FindMaxMatching(const Graph &g, int m)
{
vector<int> so1(g.size(), -1);
vector<int> so2(m, -1);
bool changes = false;
do {
changes = false;
vector<bool> visited(g.size(), false);
for (unsigned i = 0; i < g.size(); ++i) {
if (so1[i] == -1 && FindPair(g, visited, so1, so2, i)) {
changes = true;
}
}
} while (changes);
vector<pair<int, int>> matching;
for (unsigned i = 0; i < g.size(); ++i) {
if (so1[i] != -1) {
matching.push_back({i, so1[i]});
}
}
return matching;
}
int main()
{
ifstream fin("cuplaj.in");
ofstream fout("cuplaj.out");
int n, m, k;
fin >> n >> m >> k;
Graph graph(n);
while (k--) {
int x, y;
fin >> x >> y;
graph[x - 1].push_back(y - 1);
}
auto matching = FindMaxMatching(graph, m);
fout << matching.size() << "\n";
for (const auto &edge : matching) {
fout << edge.first + 1 << " " << edge.second + 1 << "\n";
}
return 0;
}