COURSES
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:
Input
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 Student 1 1 Student 1 2 ... Student 1 Count1 Count2 Student 2 1 Student 2 2 ... Student 2 Count2 ... CountP Student P 1 Student P 2 ... Student P 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. Output
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.
Sample Input Sample Output Source |
[Submit] [Go Back] [Status] [Discuss]
我的解答:
不清楚每个学生是不是一定要分到课,只考虑每个课都得分到学生,过了.二分图,匈牙利算法.
/*=============================================================================
# FileName: 1469.cpp
# Desc: poj 1469
# Author: zhuting
# Email: cnjs.zhuting@gmail.com
# HomePage: my.oschina.net/locusxt
# Version: 0.0.1
# CreatTime: 2013-12-07 18:21:46
# LastChange: 2013-12-07 18:21:46
# History:
=============================================================================*/
#include <cstdio>
#include <cstdlib>
#include <string>
#include <cstring>
#include <algorithm>
#define maxp 105 /*courses*/
#define maxn 305 /*student*/
bool mymap[maxp][maxn] = {0};
bool cover[maxn] = {0};
int link_[maxn] = {0};
int n = 0, p = 0;/*p = course_num, n = student_num*/
bool find(int x)
{
for (int i = 0; i < n; ++i)
{
if (!cover[i] && mymap[x][i])
{
cover[i] = 1;
if (link_[i] == -1 || find(link_[i]))
{
link_[i] = x;
return 1;
}
}
}
return 0;
}
void init()
{
memset(mymap, 0, sizeof(mymap));
memset(cover, 0, sizeof(cover));
memset(link_, 0xff, sizeof(link_));
return;
}
int main()
{
int t = 0;
int link__num = 0;
int tmp = 0;
scanf("%d", &t);
while(t--)
{
int ans = 0;
init();
scanf("%d%d", &p, &n);
for(int i = 0; i < p; ++i)
{
scanf("%d", &link__num);
for (int j = 0; j < link__num; ++j)
{
scanf("%d", &tmp);
mymap[i][tmp - 1] = 1;
}
}
for (int i = 0; i < p; ++i)
{
memset(cover, 0, sizeof(cover));
if (find(i))
++ans;
}
//printf("%d\n", ans);
if (ans == p) printf("YES\n");
else printf("NO\n");
}
return 0;
}