Pagini recente » Cod sursa (job #359200) | Rating Daniela Dumitrescu (d.ravoiu) | Cod sursa (job #1172664) | Cod sursa (job #1997054) | Cod sursa (job #2188271)
#include <fstream>
#include <vector>
#define DIM 10002
using namespace std;
ifstream in ("cuplaj.in");
ofstream out("cuplaj.out");
int n, m, Left[DIM], Right[DIM], muc, x, y, cuplaj_max;
bitset<DIM> viz;
vector<int> graf[DIM];
vector<pair<int, int> > sol;
bool cuplaj(int nod){
if(viz[nod] == 1)
return false;
viz[nod] = true;
for(auto nodc : graf[nod]){
if(Right[nodc] == 0){
Right[nodc] = nod;
Left[nod] = nodc;
return true;
}
}
for(auto nodc : graf[nod]){
if(cuplaj(Right[nodc])){
Right[nodc] = nod;
Left[nod] = nodc;
return true;
}
}
return false;
}
int main(int argc, const char * argv[]) {
in>>n>>m>>muc;
for(int i = 1; i <= muc; ++ i){
in>>x>>y;
graf[x].push_back(y);
}
int ok = 1;
while(ok){
ok = 0;
viz.reset();
for(int i = 1; i <= n; ++ i)
if(!Left[i])
ok |= cuplaj(i);
}
for(int i = 1; i <= n; ++ i)
if(Left[i])
sol.push_back(make_pair(i, Left[i]));
out<<sol.size()<<'\n';
for(auto it : sol)
out<<it.first<<" "<<it.second<<'\n';
return 0;
}