Pagini recente » Cod sursa (job #1106850) | Cod sursa (job #1512156) | Cod sursa (job #1172406) | Cod sursa (job #987455) | Cod sursa (job #1218792)
#include <fstream>
#include <vector>
#include <bitset>
#define DIM 10002
using namespace std;
ifstream fin("cuplaj.in");
ofstream fout("cuplaj.out");
vector<int> L[DIM];
bitset<DIM> V;
int Left[DIM], Right[DIM];
int n, m, k, x, y, i, sol, ok;
int cupleaza(int nod) {
V[nod] = 1;
// caut un vecin d-al sau necuplat
for (int i = 0; i<L[nod].size();i++) {
int x = L[nod][i];
if (Right[x] == 0) {
Left[nod] = x;
Right[x] = nod;
return 1;
}
}
for (int i = 0; i<L[nod].size();i++) {
int x = L[nod][i];
if (V[Right[x]] == 0 && cupleaza(Right[x])) {
Left[nod] = x;
Right[x] = nod;
return 1;
}
}
return 0;
}
int main() {
fin>>n>>m;
fin>>k;
for (i=1;i<=k;i++) {
fin>>x>>y;
L[x].push_back(y);
}
do {
for (i=1;i<=n;i++)
V[i] = 0;
ok = 0;
for (i=1;i<=n;i++)
if (Left[i] == 0 && cupleaza(i)) {
ok = 1;
}
} while (ok);
for (i=1;i<=n;i++) {
if (Left[i])
sol++;
}
fout<<sol<<"\n";
for (i=1;i<=n;i++)
if (Left[i]) {
fout<<i<<" "<<Left[i]<<"\n";
}
return 0;
}