luogu P2756 飞行员配对方案问题

折月煮酒 提交于 2020-02-06 17:09:28

建立一个超级源点,将每个外籍飞行员连一条capacity为1的路,一个超级汇点,每个英国飞行员也连一条capacity为1的路,根据读入在英国飞行员和外籍飞行员连接capacity为1的路,匹配方案就是最大流,遍历每一个外籍飞行员的连接,当有流时就输出即可

const int maxm = 3e4+5;
const int INF = 0x3f3f3f3f;

struct edge{
    int u, v, cap, flow, nex;
} edges[maxm<<1];

int head[maxm<<1], cur[maxm<<1], cnt, level[105];

void init() {
    memset(head, -1, sizeof(head));
}

void addedge(int u, int v, int cap) {
    edges[cnt] = edge{u, v, cap, 0, head[u]};
    head[u] = cnt++;
}

void bfs(int s) {
    memset(level, -1, sizeof(level));
    queue<int> q;
    level[s] = 0;
    q.push(s);
    while(!q.empty()) {
        int u = q.front();
        q.pop();
        for(int i = head[u]; i != -1; i = edges[i].nex) {
            edge& now = edges[i];
            if(now.cap > now.flow && level[now.v] < 0) {
                level[now.v] = level[u] + 1;
                q.push(now.v);
            }
        }
    }
}

int dfs(int u, int t, int f) {
    if(u == t) return f;
    for(int& i = cur[u]; i != -1; i = edges[i].nex) {
        edge& now = edges[i];
        if(now.cap > now.flow && level[u] < level[now.v]) {
            int d = dfs(now.v, t, min(f, now.cap - now.flow));
            if(d > 0) {
                now.flow += d;
                edges[i^1].flow -= d;
                return d;
            }

        }
    }
    return 0;
}

int dinic(int s, int t) {
    int maxflow = 0;
    for(;;) {
        bfs(s);
        if(level[t] < 0) break;
        memcpy(cur, head, sizeof(head));
        int f;
        while((f = dfs(s, t, INF)) > 0)
            maxflow += f;
    }
    return maxflow;
}

void run_case() {
    int n, m, u, v, cap;
    init();
    cin >> m >> n;
    for(int i = 1; i <= m; ++i)
        addedge(0, i, 1), addedge(i, 0, 0);
    while(cin >> u >> v && (u+v)>0) {
        addedge(u, v, 1), addedge(v, u, 0);
    }
    for(int i = m+1; i <= n; ++i)
        addedge(i, n+1, 1), addedge(n+1, i, 0);
    int maxflow = dinic(0, n+1);
    if(maxflow == 0) {
        cout << "No Solution!\n";
        return;
    }
    cout << maxflow << "\n";
    for(int u = 1; u <= m; ++u) {
        for(int i = head[u]; i != -1; i = edges[i].nex)
            if(edges[i].flow) {cout << edges[i].u << " " << edges[i].v << "\n"; break;}
    }
}

int main() {
    ios::sync_with_stdio(false), cin.tie(0);
    run_case();
    //cout.flush();
    return 0;
}
View Code

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!