1. 程式人生 > >洛谷 P2756 飛行員配對方案問題(網路流24題)

洛谷 P2756 飛行員配對方案問題(網路流24題)

思路: 二分圖匹配。

程式碼:

#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; }