Pagini recente » Cod sursa (job #2522027) | Cod sursa (job #2416533) | Cod sursa (job #811447) | Cod sursa (job #2087696) | Cod sursa (job #1690223)
#include <iostream>
#include <fstream>
#include <vector>
#include <bitset>
using namespace std;
ifstream fin ("cuplaj.in");
ofstream fout ("cuplaj.out");
const int nmax = 1e4+5;
vector <int> g[nmax];
bitset <nmax> viz;
int n, m, kid[nmax], dady[nmax];
bool cuplaj(int dad) {
if(viz[dad]==true) return false;
viz[dad]=true;
for(auto son : g[dad]) {
if(dady[son]==0) {
dady[son]=dad;
kid[dad]=son;
return true;
}
if(cuplaj(dady[son])) {
dady[son]=dad;
kid[dad]=son;
return true;
}
}
return false;
}
void cuplaj_maxim() {
int i, cup=0;
bool ok=true;
while(ok) {
ok=false;
viz=0;
for(i=1; i<=n; i++)
if(!kid[i] && cuplaj(i)) ok=true;
}
for(i=1; i<=n; i++)
if(kid[i]) cup++;
fout << cup << "\n";
for(i=1; i<=n; i++)
if(kid[i]) fout << i << " " << kid[i] << "\n";
}
int main() {
ios_base::sync_with_stdio(false);
int e, i, x, y;
fin >> n >> m >> e;
for(i=1; i<=e; i++) {
fin >> x >> y;
g[x].push_back(y);
}
cuplaj_maxim();
fin.close();
fout.close();
return 0;
}