题目描述
Consider a group of N students and P courses. Each student visits zero, one or more than one courses. Your task is to determine whether it is possible to form a committee of exactly P students that satisfies simultaneously the conditions:
- every student in the committee represents a different course (a student can represent a course if he/she visits that course)
- each course has a representative in the committee
输入格式
Your program should read sets of data from the std input. The first line of the input contains the number of the data sets. Each data set is presented in the following format:
P N
Count1 Student1 1 Student1 2 … Student1 Count1
Count2 Student2 1 Student2 2 … Student2 Count2
…
CountP StudentP 1 StudentP 2 … StudentP CountP
The first line in each data set contains two positive integers separated by one blank: P (1 <= P <= 100) - the number of courses and N (1 <= N <= 300) - the number of students. The next P lines describe in sequence of the courses �from course 1 to course P, each line describing a course. The description of course i is a line that starts with an integer Count i (0 <= Count i <= N) representing the number of students visiting course i. Next, after a blank, you抣l find the Count i students, visiting the course, each two consecutive separated by one blank. Students are numbered with the positive integers from 1 to N.
There are no blank lines between consecutive sets of data. Input data are correct.输出格式
The result of the program is on the standard output. For each input data set the program prints on a single line “YES” if it is possible to form a committee and “NO” otherwise. There should not be any leading blanks at the start of the line.
输入样例
2
3 3
3 1 2 3
2 1 2
1 1
3 3
2 1 3
2 1 3
1 1输出样例
YES
NO
分析
题目大意是给出p个课程和n个学生,每个课程有0个或多个学生代表,问是否存在一种方案,使得每个课程都有一个代表,且任意两个课程的代表均不相同。
典型的求二分图最大匹配问题,将课程作为左点集,学生作为右点集建立二分图,求最大匹配数是否为p即可,以下是dfs和bfs版本的源代码。
源程序
DFS版本
//#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#define MAXN 405
#define MAXM 30005
using namespace std;
struct Edge{
int u,v,next;
Edge(){}
Edge(int u,int v,int next):u(u),v(v),next(next){}
}edge[MAXM<<1];
int EdgeCount,head[MAXN];
int t,p,n,m,link[MAXN];
bool vis[MAXN];
void addEdge(int u,int v)
{
edge[++EdgeCount]=Edge(u,v,head[u]);
head[u]=EdgeCount;
}
bool dfs(int u)
{
for(int i=head[u];i;i=edge[i].next){
int v=edge[i].v;
if(!vis[v]){ //不在交替路中
vis[v]=true;
if(link[v]==-1 || dfs(link[v])){ //未匹配点或找到新方案
link[v]=u;
return true;
}
}
}
return false;
}
bool hargarian()
{
for(int i=1;i<=p;i++){
memset(vis,false,sizeof(vis));
if(!dfs(i)) //没找到新匹配方案
return false;
}
return true;
}
int main()
{
scanf("%d",&t);
while(t--){
memset(link,-1,sizeof(link));
memset(head,0,sizeof(head));
EdgeCount=0;
scanf("%d%d",&p,&n);
for(int u=1;u<=p;u++){
scanf("%d",&m);
for(int i=1;i<=m;i++){
int v;
scanf("%d",&v);
addEdge(u,v+p);
}
}
if(hargarian())
printf("YES\n");
else
printf("NO\n");
}
}
BFS版本
#include <cstdio>
#include <cstring>
#include <queue>
#define MAXN 405
#define MAXM 30005
using namespace std;
struct Edge{
int u,v,next;
Edge(){}
Edge(int u,int v,int next):u(u),v(v),next(next){}
}edge[MAXM<<1];
int EdgeCount,head[MAXN];
int t,p,n,m,pre[MAXN],link[MAXN],vis[MAXN];
queue<int> q;
void addEdge(int u,int v)
{
edge[++EdgeCount]=Edge(u,v,head[u]);
head[u]=EdgeCount;
}
bool hargarian()
{
memset(pre,-1,sizeof(pre));
memset(link,-1,sizeof(link));
memset(vis,-1,sizeof(vis));
int ans=0; //记录最大匹配数
for(int i=1;i<=p;i++){
if(link[i]==-1){ //尚未匹配
while(!q.empty())q.pop(); //清空队列
q.push(i);
bool flag=false;
while(!q.empty()&&!flag){ //寻找匹配方案
int u=q.front();q.pop();
for(int j=head[u];j;j=edge[j].next){
if(flag) break; //找到新匹配方案
int v=edge[j].v;
if(vis[v]!=i){ //不在交替路中
vis[v]=i;
if(link[v]>=0) //已有匹配点,寻找增广路
q.push(link[v]),pre[link[v]]=u;
else{ //找到匹配点
flag=true;
int l=u,r=v;
while(l!=-1){ //分配新方案
int tmp=link[l];
link[l]=r;
link[r]=l;
l=pre[l];
r=tmp;
}
}
}
}
}
if(link[i]==-1)
return false;
}
}
return true;
}
int main()
{
scanf("%d",&t);
while(t--){
memset(head,0,sizeof(head));
EdgeCount=0;
scanf("%d%d",&p,&n);
for(int u=1;u<=p;u++){
scanf("%d",&m);
for(int i=1;i<=m;i++){
int v;
scanf("%d",&v);
addEdge(u,v+p);
}
}
if(hargarian())
printf("YES\n");
else
printf("NO\n");
}
}