网络瘤24题 飞行员配对方案问题

洛谷P2756 飞行员配对方案问题
题目链接

首先我是个蒟蒻;

对于这道题的第一个想法就是直接跑匈牙利算法(显然)。

然而,这还要输出解QwQ,还好是SPJ,所以直接输出match就行

#include<cstdio>
#include<vector>
#include<cstring>
#include<iostream>
using namespace std;
template<class T>
inline void read(T& p) {
	char c;
	p=0;
	bool f=0;
	for(c=getchar(); c<'0'||c>'9'; c=getchar())if(c=='-')f=true;
	for(; c>='0'&&c<='9'; c=getchar()) p=(p<<3)+(p<<1)+c-'0';
	if(f)p=-p;
}
template<class T,class... Args>
inline void read(T& x,Args&... args) {
	read(x);
	read(args...);
}
template<class T>
inline void write(T x) {
	if(x<0) putchar('-'),x=-x;
	if(x>9) write(x/10);
	putchar('0'+x%10);
}
template<class T,class... Args>
inline void write(T x,Args... args) {
	write(x);
	putchar(' ');
	write(args...);
}
template<class... Args>
inline void writeln(Args... args) {
	write(args...);
	putchar('\n');
}
vector<int> dis[110];
bool vis[110];
int match[110];
int n,m,f,t;
bool find(int x) {
	for(register int i=0,to; i<dis[x].size(); i++) {
		to=dis[x][i];
		if(!vis[to]) {
			vis[to]=true;
			if(!match[to]||find(match[to])) {
				match[to]=x;
				return true;
			}
		}
	}
	return false;
}
int max_match() {
	int ans=0;
	for(register int i=1; i<=n; i++) {
		memset(vis,false,sizeof(vis));
		if(find(i)) ans++;
	}
	return ans;
}
int main() {
	read(n,m,f,t);
	while(f!=-1&&t!=-1) {
		dis[f].push_back(t);
		read(f,t);
	}
	int ans=max_match();
	if(!ans) {
		puts("No Solution!");
	} else {
		writeln(ans);
		for(register int i=1; i<=m; i++) {
			if(match[i]) writeln(match[i],i);
		}
	}
	return 0;
}
posted @ 2019-09-07 22:45  G_M_H  阅读(125)  评论(0编辑  收藏  举报