Pagini recente » Cod sursa (job #158539) | Cod sursa (job #2250043) | Cod sursa (job #2681418) | Cod sursa (job #173231) | Cod sursa (job #2950252)
#include <bits/stdc++.h>
using namespace std;
ifstream in("cuplaj.in");
ofstream out("cuplaj.out");
vector <int> a[100005];
bitset <10005> viz;
int m, n, e, st[100005], dr[100005], sol;
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] = 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] = nod;
return 1;
}
/// daca ajung aici nu se mai pot realiza cuplaje
return 0;
}
void Test_Case()
{
int i, ok, x, y;
in >> n >> m >> e;
while(e--)
{
in >> 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))
{
sol++;
ok = 0;
}
}
out << sol << "\n";
for(i = 1; i <= n; i++)
if(st[i] != 0)
out << i << " " << st[i] << "\n";
}
int main()
{
Test_Case();
return 0;
}