51nod 2006 飞行员配对(二分图最大匹配)

题目链接

#include <iostream>
#include <vector>
#include <cstring>
using namespace std;
const int N = 103;
int n, m;
vector<int> G[N];
int match[N], st[N];
bool find(int x){
	for(int j = 0; j < G[x].size(); j++){
		int v = G[x][j];
		if(!st[v]){ //每轮只能用一次 
			st[v] = 1;
			if(!match[v] || find(match[v])){ //没有对象或者是能腾出一个对象来 
				match[v] = x; //那就让她跟x好上
				return true; 
			}
		}
	}
	return false;
}
int main(){
	cin >> n>> m;
	int u, v;
	while(cin>>u>>v){
		if(u == -1 && v == -1) break;
		G[u].push_back(v);	
	}
	int res = 0;
	for(int i = 1; i <= m; i++){
		memset(st, 0 ,sizeof(st));
		if(find(i)) res++;
	}
	if(!res) cout<<"No Solution!"<<endl;
	else cout<<res<<endl;
	return 0;
}
上一篇:51Nod 2642 质数的和与积 c/c++题解


下一篇:51NOD 1006 最长公共子序列 Lcs 动态规划 DP 模板题 板子