Pagini recente » Cod sursa (job #1739131) | Cod sursa (job #651074) | Cod sursa (job #709001) | Cod sursa (job #2358463) | Cod sursa (job #2948902)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("cuplaj.in");
ofstream fout("cuplaj.out");
int n, m, e, st[10005], dr[10005], sol;
bitset <100005> viz;
vector <int> a[10005];
int Cuplaj(int nod)
{
if (viz[nod] == 1)
return 0;
viz[nod] = 1;
for (auto nod_2 : a[nod])
{
if (dr[nod_2] == 0) // daca adiacentul nu este cuplat
{
/// cuplez nod_2
st[nod] = nod_2;
dr[nod_2] = st[nod];
return 1;
}
}
/// daca ajung aici, toti adiacentii lui nod sunt deja cuplati
/// incerc sa "decuplez" fiecare dintre adiacentii lui nod
/// repetand aceeasi procedura
for (auto nod_2 : a[nod])
{
if (Cuplaj(dr[nod_2]) == 1)
{
/// cuplez nod_2
st[nod] = nod_2;
dr[nod_2] = st[nod];
return 1;
}
}
/// daca ajung aici nu se mai pot realiza cuplaje
return 0;
}
void Test_Case()
{
int x, y, i, ok;
fin >> n >> m >> e;
while (e--)
{
fin >> x >> y;
a[x].push_back(y);
}
ok = 0;
while (ok == 0)
{
ok = 1;
viz.reset();
for (i = 1; i <= n; i++)
if (st[i] == 0 and Cuplaj(i))
{
ok = 0;
sol++;
}
}
fout << sol << "\n";
for (i = 1; i <= n; i++)
if (st[i] != 0)
fout << i << " " << st[i] << "\n";
}
int main()
{
int t;
ios_base::sync_with_stdio(false);
cin.tie(0);
t = 1;
while (t--)
Test_Case();
return 0;
}