You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

81 lines
2.5 KiB
C++

#define _CRT_SECURE_NO_WARNINGS
#define _SILENCE_CXX17_C_HEADER_DEPRECATION_WARNING
#include <bits/stdc++.h>
using namespace std;
const int N = 2050, inf = 0x3f3f3f3f;
int adj[N], nxt[N << 1], to[N << 1], cap[N << 1], cur[N], cnt[N], dis[N], fa[N], ecnt;
inline void addEdge_impl_(int f, int t, int c)
{
nxt[ecnt] = adj[f];
adj[f] = ecnt;
to[ecnt] = t;
cap[ecnt] = c;
ecnt++;
}
inline void addEdge(int f, int t, int c)
{
addEdge_impl_(f, t, c);
addEdge_impl_(t, f, 0);
}
int ISAP(int S, int T)
{
int flow = 0;
for (int &di : dis) di = N - 1;
int len = 0, x;
static int que[N];
dis[que[len++] = T] = 0;
for (int i = 0; i < len; i++)
for (int e = adj[x = que[i]]; ~e; e = nxt[e])
if (cap[e ^ 1] && dis[to[e]] > dis[x] + 1)
dis[que[len++] = to[e]] = dis[x] + 1;
memset(cnt, 0, sizeof(cnt));
for (int i = 0; i < N; i++) cur[i] = adj[i], cnt[dis[i]]++;
x = S;
while (dis[S] < N - 1)
{
if (x == T)
{
int curFlow = inf;
for (x = T; x != S; x = to[fa[x] ^ 1]) curFlow = min(curFlow, cap[fa[x]]);
for (x = T; x != S; x = to[fa[x] ^ 1]) cap[fa[x]] -= curFlow, cap[fa[x] ^ 1] += curFlow;
flow += curFlow, x = S;
}
bool needRetreat = true;
for (int e = cur[x]; needRetreat && ~e; e = nxt[e])
if (cur[x] = e, cap[e] && dis[x] == dis[to[e]] + 1)
needRetreat = false, fa[x = to[e]] = e;
if (needRetreat)
{
int nd = N - 2;
for (int e = adj[x]; ~e; e = nxt[e])
if (cap[e]) nd = min(nd, dis[to[e]]);
if (--cnt[dis[x]] == 0) break;
++cnt[dis[x] = nd + 1];
cur[x] = adj[x];
if (x != S) x = to[fa[x] ^ 1];
}
}
return flow;
}
int main()
{
memset(adj, -1, sizeof(adj)), ecnt = 0;
int n, m;
scanf("%d%d", &m, &n);
for (int i, j; scanf("%d%d", &i, &j), ~(i & j);)
addEdge(i, j, 1);
for (int i = 1; i <= m; i++) addEdge(0, i, 1);
for (int j = m + 1; j <= n; j++) addEdge(j, n + 1, 1);
int flow = ISAP(0, n + 1);
if (flow)
{
printf("%d\n", flow);
for (int f = 1; f <= m; f++)
for (int e = adj[f]; ~e; e = nxt[e])
if (cap[e ^ 1] && m < to[e] && to[e] <= n)
printf("%d %d\n", f, to[e]);
}
else
puts("No Solution!");
return 0;
}