Pagini recente » Cod sursa (job #3332094) | Cod sursa (job #3342268) | Cod sursa (job #3316773) | Cod sursa (job #3326441) | Cod sursa (job #3328057)
#include <bits/stdc++.h>
using namespace std;
ifstream fin ("cuplaj.in");
ofstream fout ("cuplaj.out");
const int nmax = 1e4 + 5;
vector <int> g[nmax];
int l[nmax], r[nmax], u[nmax];
int n, m, e;
bool pairup (int node)
{
if (u[node]) return false;
u[node] = 1;
for (auto x : g[node])
{
if (!r[x])
{
l[node] = x;
r[x] = node;
return true;
}
}
for (auto x : g[node])
{
if (pairup (r[x]))
{
l[node] = x;
r[x] = node;
return true;
}
}
return false;
}
int main ()
{
fin >> n >> m >> e;
for (int i = 1; i <= e; i++)
{
int x, y;
fin >> x >> y;
g[x].push_back (y);
}
bool ok = true;
while (ok)
{
ok = false;
for (int i = 1; i <= n; i++)
u[i] = 0;
for (int i = 1; i <= n; i++)
if (!l[i])
ok |= pairup (i);
}
int cuplaj = 0;
for (int i = 1; i <= n; i++)
cuplaj += l[i] > 0;
fout << cuplaj << '\n';
for (int i = 1; i <= n; i++)
if (l[i] > 0)
fout << i << " " << l[i] << '\n';
return 0;
}