Pagini recente » Cod sursa (job #1106626) | Cod sursa (job #1317689) | Cod sursa (job #2920031) | Cod sursa (job #160577) | Cod sursa (job #2134694)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("cuplaj.in");
ofstream fout("cuplaj.out");
const int Nmax = 10005;
int n, m, e, st[Nmax], dr[Nmax];
/// st[i] = nodul cu care se cupleaza nodul i din B
/// dr[i] = nodul cu care se cupleaza nodul i din A
vector <int> L[Nmax];
int cuplajMax; /// nr de muchii din cuplajul maxim
bool viz[Nmax];/// viz[i] = 1 daca i din stanga e cuplat
int Cupleaza(int k)
{
if (viz[k] == 1) return 0; /// e deja cuplat
viz[k] = 1;
for (auto i : L[k])
if (dr[i] == 0) /// i e necuplat
{
st[k] = i;
dr[i] = k;
return 1;
}
/// n-am reusit sa-l cuplez pe k
/// deci mai parcurg o data L[k] si poate reusesc sa cuplez pe k cu un nod cuplat
for (auto i : L[k])
if (Cupleaza(dr[i]))
{
st[k] = i;
dr[i] = k;
return 1;
}
return 0;
}
void Citire()
{
fin >> n >> m >> e;
int i, x, y;
for (i = 1; i <= e; i++)
{
fin >> x >> y;
L[x].push_back(y);
}
}
void Rezolva()
{
int i, gata = 0;
while (gata == 0)
{
gata = 1;
for (i = 1; i <= n; i++)
viz[i] = 0;
for (i = 1; i <= n; i++)
if (st[i] == 0 && Cupleaza(i))
{
cuplajMax++;
gata = 0;
}
}
fout << cuplajMax << "\n";
for (i = 1; i <= n; i++)
if (st[i] != 0)
fout << i << " " << st[i] << "\n";
fout << "\n";
fout.close();
}
int main()
{
Citire();
Rezolva();
return 0;
}