飛行員配對方案問題 二分圖最大匹配
阿新 • • 發佈:2018-12-16
題目背景
第二次世界大戰時期..
題目描述
英國皇家空軍從淪陷國徵募了大量外籍飛行員。由皇家空軍派出的每一架飛機都需要配備在航行技能和語言上能互相配合的2 名飛行員,其中1 名是英國飛行員,另1名是外籍飛行員。在眾多的飛行員中,每一名外籍飛行員都可以與其他若干名英國飛行員很好地配合。如何選擇配對飛行的飛行員才能使一次派出最多的飛機。對於給定的外籍飛行員與英國飛行員的配合情況,試設計一個演算法找出最佳飛行員配對方案,使皇家空軍一次能派出最多的飛機。
對於給定的外籍飛行員與英國飛行員的配合情況,程式設計找出一個最佳飛行員配對方案,使皇家空軍一次能派出最多的飛機。
輸入輸出格式
輸入格式:
第 1 行有 2 個正整數 m 和 n。n 是皇家空軍的飛行員總數(n<100);m 是外籍飛行員數(m<=n)。外籍飛行員編號為 1~m;英國飛行員編號為 m+1~n。
接下來每行有 2 個正整數 i 和 j,表示外籍飛行員 i 可以和英國飛行員 j 配合。最後以 2個-1 結束。
輸出格式:
第 1 行是最佳飛行員配對方案一次能派出的最多的飛機數 M。接下來 M 行是最佳飛行員配對方案。每行有 2個正整數 i 和 j,表示在最佳飛行員配對方案中,飛行員 i 和飛行員 j 配對。如果所求的最佳飛行員配對方案不存在,則輸出‘No Solution!’。
輸入輸出樣例
輸入樣例#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
輸出樣例#1: 複製
4 1 7 2 9 3 8 5 10
雖說是網路流24題,但是完全可以用最大匹配來做;
因為最大匹配非常的明顯;
因為我不會網路流
#include<iostream> #include<cstdio> #include<algorithm> #include<cstdlib> #include<cstring> #include<string> #include<cmath> #include<map> #include<set> #include<vector> #include<queue> #include<bitset> #include<ctime> #include<deque> #include<stack> #include<functional> #include<sstream> //#include<cctype> //#pragma GCC optimize("O3") using namespace std; #define maxn 200005 #define inf 0x3f3f3f3f #define INF 999999999999999 #define rdint(x) scanf("%d",&x) #define rdllt(x) scanf("%lld",&x) #define rdlf(x) scanf("%lf",&x) #define rdstr(x) scanf("%s",x) typedef long long ll; typedef unsigned long long ull; typedef unsigned int U; #define ms(x) memset((x),0,sizeof(x)) const int mod = 10000007; #define Mod 20100403 #define sq(x) (x)*(x) #define eps 1e-5 typedef pair<int, int> pii; #define pi acos(-1.0) const int N = 1005; #define REP(i,n) for(int i=0;i<(n);i++) inline int rd() { int x = 0; char c = getchar(); bool f = false; while (!isdigit(c)) { if (c == '-') f = true; c = getchar(); } while (isdigit(c)) { x = (x << 1) + (x << 3) + (c ^ 48); c = getchar(); } return f ? -x : x; } ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a%b); } ll sqr(ll x) { return x * x; } int n, m; int G[1050][1050]; int vis[maxn]; int res[maxn]; int match[maxn]; bool dfs(int x) { for (int i = m + 1; i <= n; i++) { if (!vis[i] && G[x][i]) { vis[i] = 1; if (match[i] == -1 || dfs(match[i])) { match[i] = x; res[x] = i; return 1; } } } return 0; } int main() { //ios::sync_with_stdio(false); rdint(m); rdint(n); int i, j; while (cin >> i >> j && i != -1 || j != -1) { G[i][j] = 1; } memset(match, -1, sizeof(match)); int ans = 0; for (int i = 1; i <= m; i++) { ms(vis); if (dfs(i))ans++; } cout << ans << endl; for (int i = 1; i <= m; i++) { if (res[i] != 0)cout << i << ' ' << res[i] << endl; } return 0; }