Pagini recente » Cod sursa (job #535437) | Cod sursa (job #873881) | Cod sursa (job #623092) | Cod sursa (job #855716) | Cod sursa (job #1165995)
#include <algorithm>
#include <bitset>
#include <fstream>
#include <iostream>
#include <vector>
using namespace std;
ifstream fin("cuplaj.in");
ofstream fout("cuplaj.out");
const int INF = 1 << 30;
const int MAX_N = 10100;
int N, M, E;
vector<int> graph[MAX_N];
bitset<MAX_N> vis;
int l_pair[MAX_N];
int r_pair[MAX_N];
int match_size;
void read(), print();
void cuplaj();
bool try_match(int node);
void match_nodes(int x, int y);
int main() {
read();
cuplaj();
print();
return 0;
}
void read() {
fin >> N >> M >> E;
for (int i = 1, a, b; i <= E; i += 1) {
fin >> a >> b;
graph[a].push_back(b);
}
}
void cuplaj() {
bool go = 1;
while (go) {
go = 0;
vis = 0;
for (int i = 1; i <= N; i += 1)
if (!l_pair[i] && try_match(i))
match_size += 1, go = 1;
}
}
bool try_match(int node) {
if (vis[node]) return 0;
vis[node] = 1;
for (auto x: graph[node]) {
if (!r_pair[x]) {
match_nodes(node, x);
return 1;
}
}
for (auto x: graph[node]) {
if (r_pair[x] && try_match(r_pair[x])) {
match_nodes(node, x);
return 1;
}
}
return 0;
}
inline void match_nodes(int x, int y) {
l_pair[x] = y;
r_pair[y] = x;
}
void print() {
fout << match_size << '\n';
for (int i = 1; i <= N; i += 1) {
if (r_pair[l_pair[i]] == i)
fout << i << ' ' << l_pair[i] << '\n';
}
}