Pagini recente » Cod sursa (job #938036) | Cod sursa (job #1688293) | Cod sursa (job #364790) | Cod sursa (job #2557458) | Cod sursa (job #2874119)
#include <fstream>
#include <vector>
#define N_MAX 10010
using namespace std;
ifstream f("cuplaj.in");
ofstream g("cuplaj.out");
int n, m, n1, n2, e, i, x, y, nr, ok, st[N_MAX], dr[N_MAX], viz[N_MAX];
vector<int > G[N_MAX];
static inline int cupleaza(int nod)
{
if (viz[nod])
return 0;
else
{
viz[nod] = 1;
for (auto it : G[nod])
if (!dr[it] || cupleaza(dr[it]))
{
st[nod] = it;
dr[it] = nod;
return 1;
}
return 0;
}
}
static inline void cuplaj()
{
ok = 1;
while (ok)
{
for (i = 1; i <= n; ++i)
viz[i] = 0;
ok = 0;
for (i = 1; i <= n; ++i)
if (!st[i] && cupleaza(i))
ok = 1;
}
}
int main()
{
ios::sync_with_stdio(false);
f.tie(nullptr);
f >> n >> m >> e;
for (i = 1; i <= e; ++i)
{
f >> x >> y;
G[x].push_back(y);
}
cuplaj();
for (i = 1; i <= n; ++i)
if (st[i])
++nr;
g << nr << '\n';
for (i = 1; i <= n; ++i)
if (st[i])
g << i << " " << st[i] << '\n';
return 0;
}