Pagini recente » Profil unibucPoli2019 | Cod sursa (job #52) | Cod sursa (job #2984743) | Rating Ovidiu Banias (ovashin) | Cod sursa (job #2497580)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("cuplaj.in");
ofstream fout("cuplaj.out");
const int N = 1e4 + 7;
namespace Cuplaj {
int n;
int st[N], dr[N];
bool viz[N];
vector < int > adia[N];
void AddEdge(int x, int y) {
adia[x].push_back(y);
}
bool cuplaj(int nod) {
viz[nod] = 1;
for (int i : adia[nod]) {
if (!st[i]) {
st[i] = nod;
dr[nod] = i;
return 1;
}
}
for (int i : adia[nod]) {
if (!viz[st[i]] && cuplaj(st[i])) {
st[i] = nod;
dr[nod] = i;
return 1;
}
}
return 0;
}
int Build(int _n) {
n = _n;
bool act(1);
int ans(0);
while (act) {
act = 0;
fill(viz + 1, viz + n + 1, 0);
for (int i = 1; i <= n; ++i)
if (!viz[i] && !dr[i] && cuplaj(i))
++ans, act = 1;
}
return ans;
}
}
int main()
{
int n, m, k, x, y;
fin >> n >> m >> k;
while (k--) {
fin >> x >> y;
Cuplaj::AddEdge(x, y);
}
fout << Cuplaj::Build(n) << '\n';
vector < pair < int, int > > ans;
for (int i = 1; i <= n; ++i)
if (Cuplaj::dr[i])
ans.push_back({i, Cuplaj::dr[i]});
for (auto i : ans)
fout << i.first << ' ' << i.second << '\n';
return 0;
}