Pagini recente » Cod sursa (job #1030286) | Cod sursa (job #3256658) | Cod sursa (job #813733) | Cod sursa (job #2990126) | Cod sursa (job #2856137)
#include <bits/stdc++.h>
using namespace std;
ifstream f("cuplaj.in");
ofstream g("cuplaj.out");
int n,m,e;
int x,y;
vector < int > v[10005];
bool ok=1,viz[10005];
int l[10005],r[10005];
int ctr;
void read() {
f >> n >> m >> e;
for (int i=1;i<=e;i++) {
f >> x >> y;
v[x].push_back(y);
}
}
bool dfs(int nod) {
viz[nod]=1;
for (auto k:v[nod]) {
if (r[k]==0 || (viz[r[k]]==0 && dfs(r[k])==1)) {
r[k] = nod;
l[nod] = k;
return 1;
}
}
return 0;
}
void solve() {
while (ok) {
ok=0;
fill(viz+1,viz+n+1,0);
for (int i=1;i<=n;i++) {
if (l[i]==0) {
if (dfs(i)) {
ctr++;
ok=1;
}
}
}
}
}
void show() {
g << ctr << '\n';
for (int i=1;i<=n;i++) {
if (l[i]) {
g << i << " " << l[i] << '\n';
}
}
}
int main()
{
read();
solve();
show();
return 0;
}