其他分享
首页 > 其他分享> > P2756 飞行员配对方案问题

P2756 飞行员配对方案问题

作者:互联网

Descrioption

对于给定的外籍飞行员与英国飞行员的配合情况,

找出一个最佳飞行员配对方案

使皇家空军一次能派出最多的飞机

Solution

二分图最大匹配裸题

Code

#include <bits/stdc++.h>
using namespace std;
const int N = 205;
int n, m;
int cnt, head[N];
int link[N], ans;
bool vis[N];

struct Edge {
    int u, v, _next;
}G[N * N];

void addedge(int u, int v) {
    G[++cnt] = (Edge) {u, v, head[u]}, head[u] = cnt;
}

template <typename T>
void read(T &t) {
    t = 0; T m = 1; char ch = getchar();
    while(ch < '0' || ch > '9') { if(ch == '-') m = -1; ch = getchar(); }
    while(ch >= '0' && ch <= '9') { t = (t << 3) + (t << 1) + (ch & 15); ch = getchar(); }
    t *= m;
}

bool solve(int u) {
    for(int i = head[u]; i ; i = G[i]._next) {
        int v = G[i].v;
        if(vis[v]) continue;
        vis[v] = 1;
        if(link[v] == 0 || solve(link[v])) {
            link[v] = u; return true;
        }
    }
    return false;
}

int main() {
    read(m), read(n);
    while(true) {
        int a, b; read(a), read(b);
        if(a == b && a == -1) break;
        if(a > m) swap(a, b); 
        addedge(a, b);
    }
    for(int i = 1; i <= m; i++) {
        for(int j = m + 1; j <= n; j++) vis[j] = 0;
        if(solve(i)) ans++;
    }
    if(ans == 0) {
        printf("No Solution!\n");
    } else {
        printf("%d\n", ans);
        for(int i = m + 1; i <= n; i++) {
            if(link[i] != 0) {
                printf("%d %d\n", link[i], i);
            }
        }
    }
    return 0;
} 

标签:P2756,cnt,ch,int,head,while,飞行员,配对
来源: https://www.cnblogs.com/chloristendika/p/10352128.html