Pagini recente » Cod sursa (job #941047) | Cod sursa (job #3345329) | Cod sursa (job #795493) | Cod sursa (job #3347619) | Cod sursa (job #3332226)
#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], fr[nmax];
int n, m, e;
bool pairup (int node)
{
if (fr[node])
return false;
fr[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;
}
void cuplaj ()
{
bool ok = true;
while (ok)
{
ok = false;
for (int i = 1; i <= n; i++)
fr[i] = 0;
for (int i = 1; i <= n; i++)
if (!l[i])
ok |= pairup (i);
}
}
int main ()
{
fin >> n >> m >> e;
for (int i = 1; i <= e; i++)
{
int x, y;
fin >> x >> y;
g[x].push_back (y);
}
cuplaj();
int cnt = 0;
for (int i = 1; i <= n; i++)
cnt += l[i] > 0;
fout << cnt << '\n';
for (int i = 1; i <= n; i++)
if (l[i] > 0)
fout << i << " " << l[i] << '\n';
return 0;
}