# 51nod ﹣ 2006 ﹣ pilot pairing (bipartite maximum matching)

Posted by Allan- on Thu, 30 Apr 2020 22:04:24 +0200

## 51nod ﹣ 2006 ﹣ pilot pairing (bipartite maximum matching)

```Bipartite graph matching template problem
```

#### Title Description

```During the Second World War, the RAF recruited a large number of foreign pilots from the occupied countries. Each RAF aircraft needs to be equipped with two pilots who can cooperate in navigation skills and language. One is a British pilot and the other is a foreign pilot. Among the numerous pilots, each foreign pilot can cooperate well with several other British pilots. How to choose a pair of pilots to fly in order to send the most aircraft at a time. For a given cooperation between foreign pilots and British pilots, an algorithm is designed to find out the best pilot matching scheme, so that the Royal Air force can dispatch the most aircraft at a time. For a given cooperation between foreign pilots and British pilots, the program finds out an optimal pilot matching scheme, which enables the Royal Air Force to send the most aircraft at a time.
```

#### Input

```Line 1 has two positive integers m and n. N is the total number of pilots in the RAF (n < 100); m is the number of foreign pilots. Foreign pilots are numbered 1-m; British pilots are numbered m + 1-n. Next, each line has two positive integers i and j, indicating that foreign pilot I can cooperate with British pilot J. The input ends with 2 - 1.
```

#### Output

```The first line is the maximum number of aircrafts that can be dispatched by the best pilot pairing scheme at a time. Output "No Solution!" if the best pilot matching scheme is not available.
```

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

```4
```

#### Solutions to problems

```The template problem of the maximum matching of bipartite graph
```

#### AC code

```#include<iostream>
#include<vector>
#include<string.h>
using namespace std;

int V;//Number of vertices
vector<int> G[105];//The adjacency table of the graph shows G[i][j]: = pilot I can match pilot j
int match[105];//Matched vertices
bool used[105];//Access tags used in DFS

//Add an edge connecting u and v to the graph
void add_edge(int u, int v) {
G[u].push_back(v);
G[v].push_back(u);
}
//Search for the way of augmentation through dfs
bool dfs(int v) {
used[v] = true;
for (int i = 0; i < G[v].size(); ++i) {
int u = G[v][i];
int w = match[u];
if (w < 0 || !used[w] && dfs(w)) {
match[v] = u;
match[u] = v;
return true;
}
}
return false;
}
//Solving the maximum matching of bipartite graphs
int bipartite_matching() {
int res = 0;
memset(match, -1, sizeof(match));
for (int v = 0; v < V; v++) {
if (match[v] < 0) {
memset(used, 0, sizeof(used));
if (dfs(v)) {
res++;
}
}
}
return res;
}
int n, m;
void input() {
//1,m: non British pilot
//m+1,n: British pilot
int s = n;
int t = s + 1;
//Connecting the source point to the computer
cin >> m >> n;
V = m;
int ti, tj;
while (cin >> ti >> tj&&(ti!=-1||tj!=-1)) {