Description
Solution
二分匹配模板题,但是既然是练网络流,就打了个sap
Code
/**************************
Au: Hany01
Date: Jan 8th, 2018
Prob: 飞行员配对方案问题
Email: hany01@foxmail.com
**************************/
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
#define rep(i, j) for (register int i = 0, i##_end_ = (j); i < i##_end_; ++ i)
#define For(i, j, k) for (register int i = (j), i##_end_ = (k); i <= i##_end_; ++ i)
#define Fordown(i, j, k) for (register int i = (j), i##_end_ = (k); i >= i##_end_; -- i)
#define Set(a, b) memset(a, b, sizeof(a))
#define Cpy(a, b) memcpy(a, b, sizeof(a))
#define fir first
#define sec second
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
#define ALL(a) (a).begin(), (a).end()
#define SZ(a) ((int)(a).size())
#define INF (0x3f3f3f3f)
#define INF1 (2139062143)
#define Ha (1000000007)
template <typename T> inline bool chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; }
template <typename T> inline bool chkmin(T &a, T b) { return b < a ? a = b, 1 : 0; }
inline int read()
{
register int _, __; register char c_;
for (_ = 0, __ = 1, c_ = getchar(); c_ < '0' || c_ > '9'; c_ = getchar()) if (c_ == '-') __ = -1;
for ( ; c_ >= '0' && c_ <= '9'; c_ = getchar()) _ = (_ << 1) + (_ << 3) + (c_ ^ 48);
return _ * __;
}
inline void File()
{
#ifdef hany01
freopen("pilot.in", "r", stdin);
freopen("pilot.out", "w", stdout);
#endif
}
const int maxn = 105, maxm = 10005;
int n, m, s, t, e, d[maxn], gap[maxn], beg[maxn], nex[maxm], f[maxm], v[maxm];
inline void add(int uu, int vv, int ff) { v[++ e] = vv; f[e] = ff; nex[e] = beg[uu]; beg[uu] = e; }
inline void Init()
{
m = read(); n = read();
s = 0; t = n + 1; e = 1;
For(i, 1, m) add(s, i, 1), add(i, s, 0);
For(i, m + 1, n) add(i, t, 1), add(t, i, 0);
register int uu, vv;
while (scanf("%d%d", &uu, &vv) != EOF && uu != -1 && vv != -1) add(uu, vv, 1), add(vv, uu, 0);
}
int sap(int u, int flow)
{
if (u == t) return flow;
register int res = flow, tmp;
for (register int i = beg[u]; i; i = nex[i]) {
if (d[u] != d[v[i]] + 1 || !f[i]) continue;
tmp = sap(v[i], min(res, f[i]));
f[i] -= tmp; f[i ^ 1] += tmp;
if (!(res -= tmp)) return flow;
}
if (!--gap[d[u]]) d[s] = t + 1;
++ gap[++ d[u]];
return flow - res;
}
inline void Solve()
{
int ans = 0;
for (gap[0] = t + 1; d[s] < t + 1; ) ans += sap(s, INF);
if (!ans) { puts("No"); return ; }
printf("%d\n", ans);
For(i, 1, m)
for (register int j = beg[i]; j; j = nex[j]) if (!f[j] && v[j] != s) {
printf("%d %d\n", i, v[j]);
break;
}
}
int main()
{
File();
Init();
Solve();
return 0;
}
//雨暗残灯棋散后,酒醒孤枕雁来初。
// -- 杜牧《齐安郡晚秋》