Pagini recente » Cod sursa (job #1899694) | Cod sursa (job #2521294) | Cod sursa (job #1368128) | Cod sursa (job #2237276) | Cod sursa (job #2882367)
#include <bits/stdc++.h>
#define DIM 10005
using namespace std;
ifstream f("cuplaj.in");
ofstream g("cuplaj.out");
int n, m, e, L[DIM], R[DIM];
bitset <DIM> viz;
vector <int> edges[DIM];
bool cuplaj(int node)
{
if (viz[node])
return 0;
viz[node] = 1;
// cupleaza cu nod necuplat
for (auto k: edges[node])
if (!R[k])
{
R[k] = node;
L[node] = k; // 38
return 1;
}
// cupleaza cu un nod deja cuplat
for (auto k: edges[node])
if (cuplaj(R[k]))
{
R[k] = node;
L[node] = k;
return 1;
}
return 0;
}
int main()
{
f >> n >> m >> e;
for (int i = 1; i <= e; i++)
{
int x, y;
f >> x >> y;
edges[x].push_back(y);
}
bool ok = 0;
do {
ok = 0;
viz.reset();
for (int i = 1; i <= n; i++)
if (L[i] == 0)
ok |= cuplaj(i);
} while (ok == 1);
int ans = 0;
for (int i = 1; i <= n; i++)
if (L[i])
ans++;
g << ans << "\n";
for (int i = 1; i <= n; i++)
if (L[i])
g << i << " " << L[i] << "\n";
return 0;
}