zju/zoj 1140 Courses

#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;

#define M 505
int n,p,m,a,path[M];
bool map[M][M],use[M];

void init(){
	
	memset(map,false,sizeof(map));
	scanf("%d %d",&n,&p);
	
	for(int i=1;i<=n;i++){
		scanf("%d",&m);
		while(m--){
			scanf("%d",&a);
			map[i][a]=true;
		}
	}
}
bool Match(int i){
	
	for(int j=1;j<=p;j++){
		if(!use[j] && map[i][j]){
			use[j]=true;
			if(path[j]<0 || Match(path[j])){
				path[j]=i;
				return true;
			}
		}
	}
	return false;
}
void Maxmatch(){
	memset(path,-1,sizeof(path));
	int sum=0;
	for(int i=1;i<=n;i++){
		memset(use,false,sizeof(use));
		if(Match(i))
			sum++;
	}
	printf(sum==n?"YES\n":"NO\n");
}
int main(){
	int t;
	scanf("%d",&t);
	while(t--){	
		
		init();
		Maxmatch();
	}
	return 0;
}

你可能感兴趣的:(zju/zoj 1140 Courses)