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 a text file. The first line of the input file 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'll 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.
An example of program input and output:
Input
2
3 3
3 1 2 3
2 1 2
1 1
3 3
2 1 3
2 1 3
1 1
Output
YES
NO
考虑一组N个学生和P个课程。每个学生访问零,一个或多个课程。你的任务是确定是否有可能成立一个恰好由P个学生组成的委员会,同时满足以下条件:
.委员会的每个学生代表一个不同的课程(学生可以代表一个课程,如果他/她访问该课程)
.每个课程在委员会中都有一名代表
您的程序应该从文本文件中读取数据集。输入文件的第一行包含数据集的数量。每个数据集的格式如下:
P N
1 1 1 2…学生一Count1
Count2学生2 1学生2 2…学生二是从
……
学生1,学生2…StudentP CountP
每个数据集的第一行包含两个正整数,中间用一个空格隔开:P (1 <= P <= 100) -课程数量,N (1 <= N <= 300) -学生人数。接下来的P行按照课程的顺序进行描述。从课程1到课程P,每一行描述一个课程。课程i的描述是以整数Count i (0 <= Count i <= N)开头的一行,表示访问课程i的学生数量。接下来,在一个空格之后,您将看到访问课程i的学生数量,每个连续两个空格以一个空格分隔。学生的编号是从1到N的正整数。
连续数据集之间没有空行。输入数据正确。
程序的结果在标准输出上。对于每个输入数据集,程序打印在单行上“是”(如果可以组成一个委员会),“否”(否则)。在这一行的开头不应该有任何前导空格。
这一题我之所以写博客是因为建图,不论是二分图匹配的哪一题我们进行Find()的代码都是大致一样的,所以建图是二分图匹配很重要的一部分,我们要找到合理的关系进行建图。
#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
int n,p,used[305],s[305],Map[305][305];
bool Find(int x)
{
for(int i=1;i<=n;i++)
{
if(!used[i]&&Map[x][i])
{
used[i]=1;
if(!s[i]||Find(s[i]))
{
s[i]=x;
return true;
}
}
}
return false;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
memset(Map,0,sizeof(Map));
memset(s,0,sizeof(s));
int a,b;
scanf("%d%d",&p,&n);
for(int i=1;i<=p;i++)
{
scanf("%d",&a);
while(a--)
{
scanf("%d",&b);
Map[i][b]=1;//有关系的学生和科目标记
}
}
int sum=0;
for(int i=1;i<=p;i++)
{
memset(used,0,sizeof(used));
if(Find(i)) sum++;
}
if(sum==p)
printf("YES\n");
else
printf("NO\n");
}
}