Pagini recente » Cod sursa (job #3122261) | Cod sursa (job #2002475) | Cod sursa (job #1354909) | Cod sursa (job #1674663) | Cod sursa (job #1439110)
#include <stdio.h>
#include <string.h>
#include <vector>
#define MAXN 10005
using namespace std;
int n, m, nre, L[MAXN], R[MAXN];
int x, y;
vector<int> G[MAXN];
bool gata = 0, uz[MAXN];
vector< pair<int, int> > sol;
bool pairUp(int x) {
if(uz[x]) return 0;
uz[x] = 1;
for(auto y: G[x])
if(!R[y]) {
L[x] = y;
R[y] = x;
return 1;
}
for(auto y: G[x])
if(pairUp(R[y])) {
L[x] = y;
R[y] = x;
return 1;
}
return 0;
}
int main() {
freopen("cuplaj.in", "r", stdin);
freopen("cuplaj.out", "w", stdout);
int i;
scanf("%d %d %d", &n, &m, &nre);
while(nre--) {
scanf("%d %d", &x, &y);
G[x].push_back(y);
}
while(!gata) {
gata = 1;
memset(uz, 0, sizeof(uz));
for(i = 1; i <= n; i++)
if(!L[i] && pairUp(i))
gata = 0;
}
for(i = 1; i <= n; i++)
if(L[i])
sol.push_back(make_pair(i, L[i]));
printf("%d\n", sol.size());
for(i = 0; i < sol.size(); i++)
printf("%d %d\n", sol[i].first, sol[i].second);
return 0;
}