Pagini recente » Cod sursa (job #2453067) | Cod sursa (job #202863) | Cod sursa (job #1237630) | Cod sursa (job #1258474) | Cod sursa (job #2198966)
#include <bits/stdc++.h>
using namespace std;
ofstream fout("cuplaj.out");
ifstream fin("cuplaj.in");
const int NMAX = 10010;
vector<int> G[NMAX];
int r[NMAX];
int l[NMAX];
int v[NMAX];
bool match(int node){
if(v[node]) return 0;
v[node] = 1;
for(auto next : G[node]){
if(!r[next]){
r[next] = node;
l[node] = next;
return 1;
}
}
for(auto next : G[node]){
if(match(r[next])){
r[next] = node;
l[node] = next;
return 1;
}
}
return 0;
}
int main()
{
int x, y, n, m, e;
fin >> n >> m >> e;
for(int i = 1; i <= e; i++){
fin >> x >> y;
G[x].push_back(y);
}
bool ok;
do{
ok = false;
memset(v, 0, sizeof(v));
for(int i = 1; i <= n; i++){
if(!l[i] && !v[i]) ok |= match(i);
}
}while(ok);
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]) fout << i << ' ' << l[i] << '\n';
return 0;
}