Pagini recente » Cod sursa (job #2693800) | Cod sursa (job #1302640) | Cod sursa (job #2396208) | Cod sursa (job #2723049) | Cod sursa (job #2437618)
#include <iostream>
#include <fstream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <cstring>
#include <string>
#include <cmath>
#include <queue>
#include <climits>
using namespace std;
ifstream in("cuplaj.in");
ofstream out("cuplaj.out");
int go(int node, vector<bool> &vis, const vector< vector<int> > &g, vector<int> &r, vector<int> &l) {
vis[node] = 1;
for(auto it : g[node]) {
if(!l[it] || (!vis[l[it]] && go(l[it], vis, g, r, l))) {
r[node] = it;
l[it] = node;
return 1;
}
}
return 0;
}
int main() {
int n, m, e;
in >> n >> m >> e;
vector< vector<int> > g(n + 1);
for(int i = 1; i <= e; i ++) {
int x, y;
in >> x >> y;
g[x].push_back(y);
}
int ans = 0, ok = 1;
vector<int> match_to_right(n + 1, 0);
vector<int> match_to_left(m + 1, 0);
while(ok) {
ok = 0;
vector<bool> vis(n + 1, 0);
for(int i = 1; i <= n; i ++)
if(!vis[i] && !match_to_left[i])
ok += go(i, vis, g, match_to_right, match_to_left);
ans += ok;
}
out << ans << "\n";
for(int i = 1; i <= n; i ++)
if(match_to_right[i])
out << i << " " << match_to_right[i] << "\n";
return 0;
}