Pagini recente » Cod sursa (job #809511) | Cod sursa (job #2347310) | Cod sursa (job #855510) | Cod sursa (job #2906453) | Cod sursa (job #3164373)
#include <bits/stdc++.h>
#define dbg(x) cerr << (#x) << ": " << x << "\n\n";
using namespace std;
const int NMAX = 1e4 + 5, MOD = 1e9 + 7;
bool viz[NMAX], deja[NMAX];
int n, m, e, mt[NMAX], sz;
vector<int> v[NMAX];
bool kuhn (int x)
{
viz[x] = true;
for (auto u : v[x])
if (!viz[mt[u]] && (!mt[u] || kuhn(mt[u])))
{
mt[u] = x;
deja[x] = true;
return true;
}
return false;
}
signed main ()
{
#ifndef MOOLAMP
freopen("cuplaj.in", "r", stdin);
freopen("cuplaj.out", "w", stdout);
#endif
cin.tie(nullptr)->sync_with_stdio(false);
cin >> n >> m >> e;
while (e--)
{
int x, y; cin >> x >> y;
v[x].push_back(y);
}
for (int i = 1; i <= n; i++)
for (auto u : v[i])
if (!mt[u])
{
sz++;
mt[u] = i;
deja[i] = true;
break;
}
bool ce_algoritm_trash;
do
{
ce_algoritm_trash = false;
fill(viz + 1, viz + n + 1, false);
for (int i = 1; i <= n; i++)
if (!viz[i] && !deja[i] && kuhn(i))
sz++, ce_algoritm_trash = true;
} while (ce_algoritm_trash);
cout << sz << '\n';
for (int i = 1; i <= m; i++)
if (mt[i])
cout << mt[i] << ' ' << i << '\n';
}