http://poj.org/problem?id=1469
题目描述:
考虑N 个学生和P 门课程。每个学生见习0、1 或多门课程。你的任务是判断是否能从这些学生当中选出P 名学生,组成一个委员会,并同时满足以下条件:
1) 委员会中的每名学生代表一门不同的课程(如果一名学生见习了某门课程,则他/她可以代表这门课程)。
2) 每门课程在委员会中有一名代表。
分析:
只要每门课程都要学生匹配就行了,求二部图的最大匹配数是否等于课程数
//916K 422MS G++
#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
const int MAXN=305;
using namespace std;
int n,p;
vector<int> g[MAXN];
int from[MAXN],tot;
bool use[MAXN];
bool match(int x){
for(int i=0;i<g[x].size();++i){
if(!use[g[x][i]]){
use[g[x][i]]=true;
if(from[g[x][i]]==-1||match(from[g[x][i]])){
from[g[x][i]]=x;
return true;
}
}
}
return false;
}
int hungary(){
tot=0;
memset(from,255,sizeof(from));
for(int i=1;i<=p;++i){
memset(use,0,sizeof(use));
if(match(i))
tot++;
}
return tot;
}
int main()
{
int T,num,x;
cin>>T;
while(T--){
scanf("%d%d",&p,&n);
for(int i=1;i<=p;++i){
g[i].clear();
scanf("%d",&num);
for(int j=0;j<num;++j){
scanf("%d",&x);
g[i].push_back(x);
}
}
printf("%s\n",hungary()==p ?"YES":"NO");
}
return 0;
}