Pagini recente » Cod sursa (job #2122631) | Cod sursa (job #702681) | Cod sursa (job #2483401) | Cod sursa (job #260774) | Cod sursa (job #1414053)
#include <iostream>
#include <fstream>
#include <cstring>
#include <vector>
#define nmax 10001
using namespace std;
vector <int> v[nmax];
bool used[nmax];
int nL, nR, m, x, y, sol;
int L[nmax], R[nmax];
bool match(int x) {
if(used[x]) return false;
used[x] = true;
for(auto y:v[x])
if(!R[y] || match(R[y])) {
L[x] = y;
R[y] = x;
return true;
}
return false;
}
int main() {
ifstream f("cuplaj.in");
ofstream g("cuplaj.out");
f>>nL>>nR>>m;
for(int i=1; i<=m; i++) {
f>>x>>y;
v[x].push_back(y);
}
bool worth = true;
while(worth) {
worth = false;
memset(used, 0, sizeof(used));
for(int i=1; i<=nL; i++)
if(!L[i] && match(i)) {
sol++;
worth = true;
}
}
g<<sol<<"\n";
for(int i=1; i<=nL; i++)
if(L[i]) g<<i<<" "<<L[i]<<"\n";
return 0;
}