POJ T3041 Asteroids

版权声明:希望能帮到弱校的ACMer成长,因为自己是弱校菜鸡~~~~ https://blog.csdn.net/Mr__Charles/article/details/82501801

                                    POJ T3041 Asteroids


题解:

    如果没学过匈牙利算法的话,鄙人感觉会去暴力。

    匈牙利算法已经有很好的博客了,鄙人就不在这赘述了。

 

代码:

#include<cstdio>
#include<iostream>
#include<cstring>
#include<vector>
using namespace std;
const int MAXN = 505;
int n,k;
int g[MAXN];
bool line[MAXN][MAXN],used[MAXN];

void init(){
	memset(line,false,sizeof(line));
	memset(g,0,sizeof(g));
}

bool find(int x){
	for(int i = 1; i <= n; ++i){
		if(line[x][i] && !used[i]){
			used[i] = true;
			if(g[i] == 0 || find(g[i])){
				g[i] = x;
				return true;
			}
		}
	}
	return false;
}

int match(){
	int sum = 0;
	for(int i = 1; i <= n ; ++i){
		memset(used,false,sizeof(used));
		if(find(i)) sum++;
	}
	return sum;
}

int main(){
	int u,v;
	while(~scanf("%d%d",&n,&k)){
		init();
		for(int i = 1; i <= k; ++i){
			scanf("%d%d",&u,&v);
			line[u][v] = true;
		}
		printf("%d\n",match());
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/Mr__Charles/article/details/82501801