1. 程式人生 > >hdu 2444 二分圖匹配

hdu 2444 二分圖匹配

#include<cstdio>
#include<cstring>
using namespace std;
int G[220][220],vis[220],n,m,mat[220];
int dfs(int u,int flag){
	if(vis[u] != -1){
		if(vis[u] == flag) return 1;
		else return 0;
	}
	vis[u] = flag;

		for(int i = 1; i <= n; i++){
			if(G[u][i]){
				if(!dfs(i,flag^1)) return 0;
			}
		}
	return 1;
}
int match(int u){
	for( int i = 1; i <= n; i++){
		if(!vis[i] && G[u][i]){
			vis[i] = 1;
			if(mat[i] == -1 || match(mat[i])){
				mat[i] = u;
				return 1;
			}
		}
	}
	return 0;
}
int main(){
	while(scanf("%d%d",&n,&m) == 2){
		memset(G,0,sizeof(G));
		while(m--){
			int a,b;
			scanf("%d%d",&a,&b);
			G[a][b] = G[b][a] = 1;
		}
		memset(vis,-1,sizeof(vis));
		if(!dfs(1,0)) {
			printf("No\n");
			continue;
		}
		memset(mat,-1,sizeof(mat));
		int res = 0;
		for( int i = 1; i <= n; i++){
			memset(vis,0,sizeof(vis));
			if(match(i)) res++;
		}
		printf("%d\n",res/2);
	}
}