Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 17154 | Accepted: 6741 |
Description
- 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
Input
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.
Output
Sample Input
2
3 3
3 1 2 3
2 1 2
1 1
3 3
2 1 3
2 1 3
1 1
Sample Output
YES
NO
Source
1 #include <iostream>
2 #include <cstdio>
3 #include <cstring>
4 #include <cmath>
5 #include <algorithm>
6 #include <climits>
7 #include <vector>
8 #include <queue>
9 #include <cstdlib>
10 #include <string>
11 #include <set>
12 #define LL long long
13 #define INF 0x3f3f3f3f
14 using namespace std;
15 const int maxn = 310;
16 int mp[maxn][maxn],p,n;
17 int link[maxn];
18 bool used[maxn];
19 bool dfs(int u){
20 for(int i = 1; i <= n; i++){
21 if(mp[u][i] && !used[i]){
22 used[i] = true;
23 if(link[i] == -1 || dfs(link[i])){
24 link[i] = u;
25 return true;
26 }
27 }
28 }
29 return false;
30 }
31 int main(){
32 int t,temp,i,j;
33 scanf("%d",&t);
34 while(t--){
35 scanf("%d%d",&p,&n);
36 memset(mp,0,sizeof(mp));
37 for(i = 1; i <= p; i++){
38 scanf("%d",&j);
39 while(j--){
40 scanf("%d",&temp);
41 mp[i][temp] = 1;
42 }
43 }
44 int ans = 0;
45 memset(link,-1,sizeof(link));
46 for(i = 1; i <= p; i++){
47 memset(used,false,sizeof(used));
48 if(dfs(i)) ans++;
49 }
50 ans == p?puts("YES"):puts("NO");
51 }
52 return 0;
53 }