Mai intai trebuie sa te autentifici.
Cod sursa(job #225672)
| Utilizator | Data | 30 noiembrie 2008 19:30:01 | |
|---|---|---|---|
| Problema | Cuplaj maxim in graf bipartit | Scor | Ascuns |
| Compilator | cpp | Status | done |
| Runda | Marime | 1.3 kb |
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
const char iname[] = "cuplaj.in";
const char oname[] = "cuplaj.out";
#define MAXN 10005
#define FOR(i, a, b) for (int i = (int)(a); i <= (int)(b); ++ i)
#define FORI(i, V) for (vector <int>::iterator i = (V).begin(); i != (V).end(); ++ i)
vector <int> G[MAXN];
int l[MAXN], r[MAXN], u[MAXN];
int pairup(const int n)
{
if (u[n]) return 0;
u[n] = 1;
FORI (i, G[n]) if (!r[*i]) {
l[n] = *i;
r[*i] = n;
return 1;
}
FORI (i, G[n]) if (pairup(r[*i])) {
l[n] = *i;
r[*i] = n;
return 1;
}
return 0;
}
int main(void)
{
freopen(iname, "r", stdin);
freopen(oname, "w", stdout);
int n, m, cnt_edges;
scanf("%d %d %d", &n, &m, &cnt_edges);
for (; cnt_edges --; )
{
int x, y;
scanf("%d %d", &x, &y);
G[x].push_back(y);
}
for (int change = 1; change; )
{
change = 0;
memset(u, 0, sizeof(u));
FOR (i, 1, n) if (!l[i])
change |= pairup(i);
}
int cuplaj = 0;
FOR (i, 1, n) cuplaj += (l[i] > 0);
printf("%d\n", cuplaj);
FOR (i, 1, n) if (l[i] > 0)
printf("%d %d\n", i, l[i]);
return 0;
}
