Pagini recente » Cod sursa (job #3032398) | Cod sursa (job #2127385) | Cod sursa (job #2389330) | Cod sursa (job #482917) | Cod sursa (job #1894931)
#include<cstdio>
#include<vector>
#include<cstring>
using namespace std;
FILE *fin = freopen("cuplaj.in", "r", stdin);
FILE *fout = freopen("cuplaj.out", "w", stdout);
const int MAX_N = 10005;
int N, M, E;
int cuplaj;
int left[MAX_N], right[MAX_N];
bool used[MAX_N];
vector < int > G[MAX_N];
int PairUp(int X)
{
if (used[X])
return 0;
used[X] = 1;
vector < int > :: iterator it;
for (it = G[X].begin(); it != G[X].end(); it++)
if (!right[*it])
{
left[X] = *it;
right[*it] = X;
return 1;
}
for (it = G[X].begin(); it != G[X].end(); it++)
if (PairUp(right[*it]))
{
left[X] = *it;
right[*it] = X;
return 1;
}
return 0;
}
int main()
{
scanf("%d%d%d", &N, &M, &E);
for (int i = 1, u, v; i <= E; i++)
{
scanf("%d%d", &u, &v);
G[u].push_back(v);
}
int sol = 1;
while (sol)
{
sol = 0;
memset(used, 0, sizeof(used));
for (int i = 1; i <= N; i++)
if (!left[i])
sol |= PairUp(i);
}
for (int i = 1; i <= N; i++)
cuplaj += (left[i] > 0);
printf("%d\n", cuplaj);
for (int i = 1; i <= N; i++)
if (left[i])
printf("%d %d\n", i, left[i]);
}