[网络流24题]P2756 飞行员配对方案


匈牙利
https://www.luogu.com.cn/problem/P2756

这种题,除了读懂题意和看对范围之外没什么难的

点击查看代码
#include 
#define endl '\n'
#define IOS                  \
    ios::sync_with_stdio(0); \
    cin.tie(0);              \
    cout.tie(0)
#define P pair
typedef long long ll;
using namespace std;

const int N = 200 + 5;
const int INF = 0x3f3f3f3f;
vector mp[N];

int n, m, vis[N], link[N];

int dfs(int u) {
    for (auto v : mp[u]) {
        if (vis[v]) continue;
        vis[v] = 1;
        if (!link[v] || dfs(link[v])) {
            link[v] = u;
            return 1;
        }
    }
    return 0;
}
int main() {
    cin >> n >> m;
    int u, v;
    int ans = 0;
    while (cin >> u >> v && u != -1) {
        if (u <= n && v > n) mp[u].push_back(v);
        // mp[v].push_back(u);
    }
    for (int i = 1; i <= n; i++) {
        memset(vis, 0, sizeof vis);
        if (dfs(i)) {
            ans++;
        }
    }
    cout << ans << endl;
    for (int i = n + 1; i <= m; i++) {
        // cout << i << ": " << link[i] << endl;
        if (link[i]) {
            cout << link[i] << " " << i << endl;
        }
    }
    // for (int i = 1; i <= n; i++) cout << link[i] << endl;
    return 0;
}