Network flow - bipartite graph matching - pilot pairing scheme problem - network flow question 24 - Luogu P2756

Posted by daven on Fri, 24 Dec 2021 17:14:17 +0100

Network flow - bipartite graph matching - pilot pairing scheme problem - network flow question 24 - Luogu P2756

Title Description

There are n pilots in total, including M foreign pilots and (n - m) British pilots. Foreign pilots are numbered from 1 to m and British pilots are numbered from m + 1 to n. For a given cooperation between foreign pilots and British pilots, try to design an algorithm to find the best pilot matching scheme, so that the Royal Air force can send the most aircraft at a time.

Input format

The first line of input is two positive integers separated by spaces, representing the number of foreign pilots m and the total number of pilots n respectively.
From the second line to the penultimate line, there are two integers u and V in each line, which represent foreign pilots u and British pilots v.
The last line of input is guaranteed to be - 1, indicating the end of input.

Output format

Special Judge exists in this question.

Please output the maximum number of aircraft that can be sent and give a feasible scheme.
The first line of the output is an integer representing the maximum number of aircraft that can be sent at a time. Let this integer be k.
From line 2 to line k + 1, two integers u are output in each line, and v represents the cooperation between foreign pilot u and British pilot v in the scheme you give. The U and v of this k line should be different from each other.

Input and output samples
Enter #1

5 10
1 7
1 8
2 6
2 9
2 10
3 7
3 8
4 7
4 8
5 10
-1 -1

Output #1

4
1 7
2 9
3 8
5 10

Description / tips
[data scope and agreement]

yes to 100 % of number according to , protect card 1 ≤ m ≤ n < 100 , 1 ≤ u ≤ m < v ≤ n , with one group match yes shut system only meeting to Out one second . For 100 \% data, ensure that 1 \ Leq m \ Leq n < 100, 1 \ Leq u \ Leq m < V \ Leq n, and the same set of pairing relationship will be given only once. For 100% data, ensure 1 ≤ m ≤ n < 100, 1 ≤ u ≤ m < V ≤ n, and the pairing relationship of the same group will be given only once.

analysis:

two branch chart of most large Horse match ask topic . The maximum matching problem of bipartite graphs. The maximum matching problem of bipartite graphs.

mutually even meet of two spot of between even one strip power value by 1 of just towards edge , one strip power value by 0 of back towards edge , protect card Yes each individual spot only And one individual spot Horse match . The two connected points are connected with a forward edge with a weight of 1 and a reverse edge with a weight of 0, which ensures that each point matches only one point. The two connected points are connected with a forward edge with a weight of 1 and a reverse edge with a weight of 0, which ensures that each point matches only one point.

source spot S towards one side of place have spot ( Outside Nationality fly that 's ok member ) even meet one strip power value by 1 of just towards edge , power value by 0 of back towards edge All points (foreign pilots) on the side of the source point S are connected with a positive edge with a weight of 1 and a reverse edge with a weight of 0 All points (foreign pilots) on the side of the source point S are connected with a positive edge with a weight of 1 and a reverse edge with a weight of 0

another one side of place have spot ( Britain country fly that 's ok member ) towards Remit spot T even meet one strip power value by 1 of just towards edge , power value by 0 of back towards edge All points on the other side (British pilots) connect a forward edge with a weight of 1 and a reverse edge with a weight of 0 to the sink point T All points on the other side (British pilots) connect a forward edge with a weight of 1 and a reverse edge with a weight of 0 to the sink point T

that Do you one strip remnant stay network Collaterals Just build good Yes . Then a residual network will be built. Then a residual network will be built.

stay this individual network Collaterals in run most large flow , Should most large flow Just yes answer one individual most large Horse match . If the maximum flow is run in this network, the maximum flow corresponds to a maximum matching. If the maximum flow is run in this network, the maximum flow corresponds to a maximum matching.

however after I Guys Times calendar remnant stay network Collaterals in of place have edge , if this strip edge full flow , And this strip edge of end spot Compile number yes fly that 's ok member of Compile number , Then we traverse all the edges in the residual network. If this edge is full and the endpoint number of this edge is the pilot's number, Then we traverse all the edges in the residual network. If this edge is full and the endpoint number of this edge is the pilot's number,

that Do you this strip edge Just yes one group Horse match , transport Out this strip edge of two individual end spot Namely can . Then this edge is a set of matches. Just output the two endpoints of this edge. Then this edge is a set of matches. Just output the two endpoints of this edge.

code:

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>

using namespace std;

const int N = 210, M = N * N, inf = 0x3f3f3f3f;

int n, m, S, T;
int e[M], ne[M], f[M], h[N], idx;
int cur[N], q[N], d[N];

void add(int a, int b, int c)
{
    e[idx] = b, ne[idx] = h[a], f[idx] = c, h[a] = idx ++;
}

void build()
{
    memset(h, -1, sizeof h);
    for(int i = 1; i <= m; i ++) add(S, i, 1), add(i, S, 0);
    for(int i = m + 1; i <= n; i ++) add(i, T, 1), add(T, i, 0);
    int a, b;
    while(~scanf("%d%d", &a, &b), a != -1 || b != -1)
    {
        add(a, b, 1), add(b, a, 0);
    }
}

bool bfs()
{
    int hh = 0, tt = -1;
    memset(d, -1, sizeof d);
    d[S] = 0, q[++ tt] = S, cur[S] = h[S];
    
    while(hh <= tt)
    {
        int u = q[hh ++];
        
        for(int i = h[u]; ~i; i = ne[i])
        {
            int v = e[i];
            
            if(d[v] == -1 && f[i] > 0)
            {
                d[v] = d[u] + 1;
                cur[v] = h[v];
                if(v == T) return true;
                q[++ tt] = v;
            }
        }
    }
    return false;
}

int find(int u, int limit)
{
    if(u == T) return limit;
    
    int flow = 0, t;
    for(int i = cur[u]; ~i && flow < limit; i = ne[i])
    {
        int v = e[i];
        cur[u] = i;
        
        if(d[v] == d[u] + 1 && f[i] > 0)
        {
            t = find(v, min(f[i], limit - flow));
            if(!t) d[v] = -1;
            f[i] -= t, f[i ^ 1] += t, flow += t;
        }
    }
    return flow;
}

int dinic()
{
    int res = 0, flow;
    while(bfs()) while(flow = find(S, inf)) res += flow;
    return res;
}

int main()
{
    scanf("%d%d", &m, &n);
    S = 0, T = n + 1;
    build();
    
    printf("%d\n", dinic());
    for(int i = 0; i < idx; i += 2)
    {
        int j = e[i];
        if(f[i] == 0 && j >= m + 1 && j <= n)
            printf("%d %d\n", e[i + 1], j);
   
    return 0;
}

Topics: Algorithm Graph Theory network-flows acm