Pagini recente » Cod sursa (job #403523) | Cod sursa (job #2813522) | Cod sursa (job #2163284) | Cod sursa (job #1674323) | Cod sursa (job #1154922)
#include <fstream>
#include <vector>
using namespace std;
ifstream fin ("cuplaj.in");
ofstream fout ("cuplaj.out");
const int N = 1e5 + 5;
int A[N], B[N];
vector <short> g[N];
vector <bool> viz(N, 0);
int a, b, e, sol;
bool match(int x) {
if (viz[x])
return 0;
viz[x] = 1;
for (vector <short> :: iterator it = g[x].begin(); it != g[x].end(); ++it)
if (!B[*it]) {
B[*it] = x;
A[x] = *it;
return 1;
}
for (vector <short> :: iterator it = g[x].begin(); it != g[x].end(); ++it)
if (match(B[*it])) {
A[x] = *it;
B[*it] = x;
return 1;
}
return 0;
}
int main() {
fin >> a >> b >> e;
for (int x, y, i = 0; i < e; ++i) {
fin >> x >> y;
g[x].push_back (y);
}
bool done = 1;
while (done) {
done = 0;
viz.assign(a + 1, 0);
for (int i = 1; i <= a; ++i)
if (!A[i])
done |= match(i);
}
for (int i = 1; i <= a; ++i)
sol += A[i] > 0;
fout << sol << "\n";
for (int i = 1; i <= a; ++i)
if (A[i])
fout << i << " " << A[i] << "\n";
}