Pagini recente » Cod sursa (job #1183778) | Cod sursa (job #1209589) | Cod sursa (job #950760) | Cod sursa (job #1319059) | Cod sursa (job #1007341)
#include <fstream>
#include <iostream>
#include <vector>
#include <cstring>
#include <queue>
using namespace std;
const int NMAX = 10003;
vector<int> G[NMAX];
int n, m;
int st[NMAX], dr[NMAX];
bool viz[NMAX];
void read_data()
{
ifstream fin("cuplaj.in");
int e;
fin >> n >> m >> e;
for (int i = 0; i < e; ++i) {
int x, y;
fin >> x >> y;
G[x].push_back(y);
}
}
bool pairup(const int x)
{
if (viz[x])
return false;
viz[x] = true;
vector<int>::iterator it;
for (it = G[x].begin(); it!=G[x].end(); ++it)
if (!dr[*it]){
dr[*it] = x;
st[x] = *it;
return true;
}
for (it = G[x].begin(); it!=G[x].end(); ++it)
if (pairup(dr[*it])){
st[x] = *it;
dr[*it] = x;
return true;
}
return false;
}
void cuplaj()
{
bool changed = true;
while (changed) {
changed = false;
memset(viz, 0, sizeof(viz));
for (int i = 1; i <= n; ++i)
if (!st[i])
changed |= pairup(i);
}
}
int main()
{
read_data();
cuplaj();
int cuplaj = 0;
for (int i = 1; i <= n; ++i)
cuplaj += st[i] > 0;
ofstream fout("cuplaj.out");
fout << cuplaj << endl;
for (int i = 1; i <= n; ++i)
if (st[i])
fout << i << " " << st[i] << "\n";
return 0;
}