Pagini recente » Cod sursa (job #2532216) | Cod sursa (job #2839188) | Cod sursa (job #403141) | Cod sursa (job #2951811) | Cod sursa (job #2663134)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("cuplaj.in");
ofstream fout("cuplaj.out");
const int nmax = 10005;
int n, m, e, viz[nmax], l[nmax], r[nmax];
vector <int> G[nmax];
bool pairup(int nod){
if (viz[nod]) return false;
viz[nod] = true;
for (auto it : G[nod]){
if (r[it] == 0){
l[nod] = it;
r[it] = nod;
return true;
}
}
for (auto it : G[nod]){
if (pairup(r[it])){
l[nod] = it;
r[it] = nod;
return true;
}
}
return false;
}
int main(){
fin >> n >> m >> e;
for (int i = 1; i <= e; ++i){
int x, y;
fin >> x >> y;
G[x].push_back(y);
}
bool ok = true;
while (ok){
ok = false;
memset(viz, 0, sizeof viz);
for (int i = 1; i <= n; ++i)
if (l[i] == 0)
ok |= pairup(i);
}
int cuplaj = 0;
for (int i = 1; i <= n; ++i) if (l[i]) ++cuplaj;
fout << cuplaj << "\n";
for (int i = 1; i <= n; ++i) if (l[i]) fout << i << " " << l[i] << "\n";
fin.close();
fout.close();
return 0;
}