Pagini recente » Cod sursa (job #279952) | Cod sursa (job #2137885) | Cod sursa (job #2488769) | Cod sursa (job #2959525) | Cod sursa (job #1181031)
#include <cstdio>
#include <string>
#include <cmath>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define MAX 10001
vector<int>edge[MAX];
int n, m, e, x, y, st[MAX], dr[MAX];
bool viz[MAX];
bool dfs(int x) {
if (viz[x]) {
return false;
}
viz[x] = 1;
for (int i = 0; i < edge[x].size(); i++) {
int y = edge[x][i];
if (dr[y] == 0 || dfs(dr[y])) {
st[x] = y;
dr[y] = x;
return true;
}
}
return false;
}
int main()
{
freopen("cuplaj.in","r",stdin);
freopen("cuplaj.out","w",stdout);
scanf("%d %d %d", &n, &m, &e);
for (int i = 1; i <= e; i++) {
scanf("%d %d", &x, &y);
edge[x].push_back(y);
}
bool more = true;
int match = 0;
while (more) {
more = false;
memset(viz,0,sizeof(viz));
for (int i = 1; i <= n; i++)
if (st[i] == 0 && dfs(i)){
more = true;
match++;
}
}
printf("%d\n", match);
for (int i = 1; i <= n; i++)
if (st[i]){
printf("%d %d\n", i, st[i]);
}
return 0;
}