版权声明:欢迎转载欢迎评论! https://blog.csdn.net/rabbit_ZAR/article/details/82778740
题目:飞行员配对方案问题
思路:
二分图匹配。
代码:
#include<bits/stdc++.h>
using namespace std;
#define maxn 100
#define read(x) scanf("%d",&x);
int m,n;
vector<int> g[maxn+5];
int match[maxn+5];
bool use[maxn+5];
void readin() {
read(m);
read(n);
int x,y;
do {
read(x);
read(y);
g[x].push_back(y);
g[y].push_back(x);
} while (~x);
}
bool dfs(int x) {
for(int i=0; i<g[x].size(); i++) {
int y=g[x][i];
if(use[y]) continue;
use[y]=true;
if(!match[y]||dfs(match[y])) {
match[y]=x;
return true;
}
}
return false;
}
int main() {
readin();
for(int i=1; i<=m; i++) {
memset(use,0,sizeof(use));
if(!match[i]) dfs(i);
}
int s=0;
for(int i=1; i<=n; i++) {
if(match[i]) {
s++;
}
}
printf("%d\n",s);
for(int i=1; i<=n; i++) {
if(match[i]) {
printf("%d %d\n",match[i],i);
}
}
if(!s) printf("No Solution!");
return 0;
}