Pagini recente » Cod sursa (job #1881049) | Cod sursa (job #957055) | Cod sursa (job #1253540) | Cod sursa (job #2593391) | Cod sursa (job #2721089)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("cuplaj.in");
ofstream fout("cuplaj.out");
const int nmax = 10005;
int n, m, e, l[nmax], r[nmax];
bool viz[nmax];
vector <int> G[nmax];
bool pairup(int nod){
viz[nod] = true;
for (auto it : G[nod]){
if (r[it] == 0){
l[nod] = it;
r[it] = nod;
return true;
}
}
bool ok = false;
for (auto it : G[nod]){
if (viz[r[it]] == false){
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 contor = 0;
for (int i = 1; i <= n; ++i){
if (l[i]) ++contor;
}
fout << contor << "\n";
for (int i = 1; i <= n; ++i){
if (l[i]){
fout << i << " " << l[i] << "\n";
}
}
fin.close();
fout.close();
return 0;
}