Pagini recente » Cod sursa (job #2170232) | Cod sursa (job #29042) | Cod sursa (job #2732945) | Cod sursa (job #2049044) | Cod sursa (job #2837545)
#define d(x) std::cout << x << std::endl
#define dm(msg, x) std::cout << msg << x << std::endl
#define da(a, n) std::cout << "Array: ["; for (int _ = 1; _ <= n; ++_) {std::cout << a[_] << ' ';} std::cout << "]\n";
#define dma(m, a, n) std::cout << m << "["; for (int _ = 1; _ <= n; ++_) {std::cout << a[_] << ' ';} std::cout << "]\n";
#define all(a) a.begin(), a.end()
#define range(a, l, r) a.begin() + l, a.begin() + r
#define aall(a, n) a + 1, a + 1 + n
#define arange(a, l, r) a + l, a + r
#define maxself(a, b) a = std::max(a, b);
#define minself(a, b) a = std::min(a, b);
#define inout(f) std::ifstream in((f) + (std::string) ".in");std::ofstream out((f) + (std::string) ".out")
#include <iostream>
#include <fstream>
#include <vector>
#include <cstring>
const int NMAX = 1e4;
int n, m, e;
std::vector<int> graph[1 + NMAX]; // left to right
int matchLeft[1 + NMAX];
int matchRight[1 + NMAX];
bool vis[1 + NMAX];
bool match(int left) {
if (vis[left])
return false;
vis[left] = true;
for (int right : graph[left]) {
if (matchLeft[right] == 0) {
matchRight[left] = right;
matchLeft[right] = left;
return true;
}
}
for (int right : graph[left]) {
if (match(matchLeft[right])) {
matchRight[left] = right;
matchLeft[right] = left;
return true;
}
}
return false;
}
int findBestMatching() {
while (true) {
memset(vis, 0, sizeof(vis));
bool shouldContinue = false;
for (int i = 1; i <= n; ++i) {
if (matchRight[i] == 0)
shouldContinue |= match(i);
}
if (!shouldContinue)
break;
}
int ans = 0;
for (int i = 1; i <= n; ++i) {
if (matchRight[i] != 0)
++ans;
}
return ans;
}
int main() {
inout("cuplaj");
in >> n >> m >> e;
while (e--) {
int a, b;
in >> a >> b;
graph[a].push_back(b);
}
int cnt = findBestMatching();
out << cnt << '\n';
for (int i = 1; i <= n; ++i) {
if (matchRight[i])
out << i << ' ' << matchRight[i] << '\n';
}
return 0;
}