Pagini recente » Cod sursa (job #1979798) | Cod sursa (job #360897) | Cod sursa (job #125932) | Cod sursa (job #1190946) | Cod sursa (job #2210238)
#include <fstream>
#include <vector>
#include <bitset>
#define NMAX 10001
using namespace std;
vector<int> v[NMAX];
bitset<NMAX> viz;
int n, m, e, cuplaje;
int left[NMAX], right[NMAX];
bool cuplaj(int nod)
{
if (viz[nod])
return false;
viz[nod] = true;
for (auto it : v[nod])
if (!right[it])
{
left[nod] = it;
right[it] = nod;
++cuplaje;
return true;
}
for (auto it : v[nod])
if (cuplaj(right[it]))
{
left[nod] = it;
right[it] = nod;
return true;
}
return false;
}
int main()
{
ifstream fin("cuplaj.in");
fin >> n >> m >> e;
int x, y;
for (int i = 0; i < e; i++)
{
fin >> x >> y;
v[x].push_back(y);
}
fin.close();
bool found = false;
do
{
viz.reset();
for (int nod = 1; nod <= n; nod++)
ok = !left[nod] ? ok | cuplaj(nod) : ok;
}
while (ok);
ofstream fout("cuplaj.out");
fout << cuplaje << '\n';
for(int nod = 1; nod <= n; nod++)
fout << nod << " " << left[nod] << '\n';
fout.close();
return 0;
}