Pagini recente » Cod sursa (job #2656048) | Cod sursa (job #2964699) | Cod sursa (job #1831119) | Cod sursa (job #2332455) | Cod sursa (job #2913681)
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, V) for (vector <int>::iterator i = (V).begin(); i != (V).end(); ++ i)
vector <int> G[10005];
int l[10005], r[10005], u[10005];
int pairup(const int n)
{
if (u[n])
return 0;
u[n] = 1;
FOR (i, G[n])
if (!r[*i])
{
l[n] = *i;
r[*i] = n;
return 1;
}
FOR (i, G[n])
if (pairup(r[*i]))
{
l[n] = *i;
r[*i] = n;
return 1;
}
return 0;
}
int main(void)
{
ifstream fin("cuplaj.in");
ofstream fout("cuplaj.out");
int n, m, cnt_edges;
fin >> n >> m >> cnt_edges;
for (int i = 1; i <= cnt_edges; i++)
{
int x, y;
fin >> x >> y;
G[x].push_back(y);
}
int change = 1;
while (change)
{
change = 0;
memset(u, 0, sizeof(u));
for(int i = 1; i <= n; i++)
if (!l[i])
change |= 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;
}