Courses
Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 3247 Accepted Submission(s): 1555
Problem Description
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:
二分图匹配。求出课程集合到学生集合的最大匹配,如果匹配数等于课程数,则YES。
AC CODE
. 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:
二分图匹配。求出课程集合到学生集合的最大匹配,如果匹配数等于课程数,则YES。
AC CODE
/*
* 以课程为左集合,学生为右集合,求左集合到右集合的最大匹配。
*/
#include <iostream>
#include <vector>
#include <cstdio>
#include <cstring>
using namespace std;
#define maxp 101
#define maxn 301
int match[maxn]; //右集合中点i匹配左集合中点match[i],若i尚未覆盖,match[i]=-1
vector<int> g[maxp]; //存储图的邻接矩阵
bool used[maxn]; //在寻找增广路径时标记点是否已使用
int n, p;
//寻找增广路径
bool DFS(int x)
{
for (int i = 0; i < g[x].size(); ++i)
{
int v = g[x].at(i);
if (used[v] == false)
{
used[v] = true;
//如果match[v]==-1表明右集合的v未覆盖,找到增广路,否则向下继续搜索
if (match[v] == -1 || DFS(match[v]))
{
match[v] = x;
return true;
}
}
}
return false;
}
int hungry()
{
//cnt记录匹配数
int cnt = 0;
memset (match, -1, sizeof(match));
for (int i = 0; i < p; ++i)
{
memset(used, false, sizeof(used));
cnt += (int)DFS(i);
}
return cnt;
}
int main()
{
int t;
scanf("%d", &t);
while (t--)
{
scanf("%d %d", &p, &n);
for (int i = 0; i < p; ++i)
{
g[i].clear();
}
for (int i = 0; i < p; ++i)
{
int cnt;
scanf("%d", &cnt);
while (cnt --)
{
int x;
scanf("%d", &x);
g[i].push_back(x);
}
}
if (hungry() == p)
{
puts("YES");
}
else
{
puts("NO");
}
}
return 0;
}