Pagini recente » Istoria paginii utilizator/robert.poziumschi | Istoria paginii runda/igorj_10/clasament | Cod sursa (job #456201) | tema | Cod sursa (job #1860682)
#include <fstream>
#include <vector>
#include <cstring>
using namespace std;
ifstream f("cuplaj.in");
ofstream g("cuplaj.out");
int n, m, e, i, j, x, y, nr;
int st[10005], dr[10005];
bool ok, viz[10005];
vector <int> ls[10005];
bool uneste(int x) {
if (viz[x])
return 0;
int i, l = ls[x].size(), y;
viz[x] = 1;
for (i = 0; i < l; i++) {
y = ls[x][i];
if (dr[y] == 0) {
st[x]=y;
dr[y]=x;
return 1;
}
}
for (i = 0; i < l; i++) {
y = ls[x][i];
if (uneste(dr[y])) {
st[x]=y;
dr[y]=x;
return 1;
}
}
return 0;
}
int main() {
f >> n >> m >> e;
while (e--) {
f >> x >> y;
ls[x].push_back(y);
}
ok = 1;
while (ok) {
ok = nr = 0;
memset(viz, 0, sizeof(viz));
for (i = 1; i <= n; i++)
if (st[i] == 0 && uneste(i))
ok = 1, nr++;
}
for (i = 1; i <= n; i++)
nr+=(st[i]>0);
g << nr << '\n';
for (i = 1; i <= n; i++)
if (st[i])
g << i << ' ' << st[i] << '\n';
return 0;
}