Pagini recente » Cod sursa (job #3230819) | Cod sursa (job #168291) | Cod sursa (job #2740274) | Cod sursa (job #2719052) | Cod sursa (job #1549424)
#include <stdio.h>
#include <stdlib.h>
int *l, *c;
int **v;
int n, na, lung;
bool *viz;
void init();
void destroy();
bool dfs(int i);
void input();
int main() {
freopen("cuplaj.in", "r", stdin);
freopen("cuplaj.out", "w", stdout);
bool ok;
int i, j;
input();
/*for (i = 0; i < n; ++i) {
printf("Noduri %d: ", i);
for (j = 0; j < l[i]; ++j)
printf("%d ", v[i][j]);
printf("\n");
}*/
for (i = 0; i < n; ++i)
c[i] = -1;
// cuplajul maximal
for (i = 0; i < na; ++i)
if (c[i] == -1)
for (j = 0; j < l[i]; ++j)
if (c[v[i][j]] == -1) {
c[i] = v[i][j];
c[v[i][j]] = i;
++lung;
break;
}
/*for (i = 0; i < n; ++i)
printf("%2d ", i);
printf("\n");
for (i = 0; i < n; ++i)
printf("%2d ", c[i]);
printf("\n");*/
// cuplajul maxim
ok = true;
while (ok) {
ok = false;
for (i = 0; i < n; ++i)
viz[i] = false;
for (i = 0; i < na; ++i)
if (!viz[i] && c[i] == -1)
if (dfs(i)) {
ok = true;
++lung;
}
}
// afisare
printf("%d\n", lung);
for (i = 0; i < na; ++i)
if (c[i] != -1)
printf("%d %d\n", i + 1, c[i] - na + 1);
destroy();
return 0;
}
bool dfs(int i) {
viz[i] = true;
for (int j = 0; j < l[i]; ++j) {
int k = v[i][j];
if (viz[k] || c[i] == k)
continue;
if (c[k] == -1) {
c[k] = i;
c[i] = k;
return true;
}
else {
viz[k] = true;
if (dfs(c[k])) {
c[i] = k;
c[k] = i;
return true;
}
else
continue;
}
} // for j
return false;
}
void input() {
int e, i, x, y;
int a[100000], b[100000];
scanf("%d %d %d", &na, &n, &e);
n += na;
init();
for (i = 0; i < e; ++i) {
scanf("%d %d", &x, &y);
a[i] = x - 1;
b[i] = na + y - 1;
++l[a[i]];
++l[b[i]];
if (b[i] == 8)
i = i;
}
for (i = 0; i < n; ++i) {
v[i] = (int*)calloc(l[i], sizeof(int));
l[i] = 0;
}
for (i = 0; i < e; ++i) {
v[a[i]][l[a[i]]++] = b[i];
v[b[i]][l[b[i]]++] = a[i];
if (b[i] == 8)
;
}
}
void init() {
l = (int*)calloc(n, sizeof(int));
c = (int*)calloc(n, sizeof(int));
v = (int**)calloc(n, sizeof(int*));
viz = (bool*)calloc(n, sizeof(bool));
}
void destroy() {
free(l);
free(c);
for (int i = 0; i < n; ++i)
free(v[i]);
free(v);
free(viz);
}