COURSES
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 23592 | Accepted: 9238 |
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
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 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.
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
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
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
题目大意
有一些课程,课程有一些同学参加。现在给每个课程匹配一个代表,使得每个课程都有一个代表,每个学生都代表一个课程(没有重复的)。
问是否能够完全匹配。
思路
将所有课程与学生进行匹配
二分匹配啊
用匈牙利算法。之前已经普及过了
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<iostream> 2 #include<algorithm> 3 #include<cmath> 4 #include<cstdio> 5 #include<cstdlib> 6 #include<cstring> 7 8 using namespace std; 9 10 int read() 11 { 12 char ch=getchar(); 13 int x=0,f=1; 14 while(ch<'0' || ch>'9'){if(ch=='-')f=-1;ch=getchar();} 15 while(ch>='0' && ch<='9'){x=x*10+ch-'0';ch=getchar();} 16 return x*f; 17 } 18 int n,c,p,tmp,tmp2,con[301],ans; 19 bool g[301][301],used[301]; 20 bool dfs(int u) 21 { 22 for(int v=1;v<=p;v++) 23 { 24 if(g[u][v]==1 && !used[v]) 25 { 26 used[v]=1; 27 if(con[v]==-1 || dfs(con[v])) 28 { 29 con[v]=u; 30 return true; 31 } 32 } 33 } 34 return false; 35 } 36 int hungary() 37 { 38 int res=0; 39 memset(con,-1,sizeof(con)); 40 for(int j=1;j<=c;j++) 41 { 42 memset(used,0,sizeof(used)); 43 if(dfs(j))res++; 44 } 45 return res; 46 } 47 int main() 48 { 49 n=read(); 50 for(int i=1;i<=n;i++) 51 { 52 memset(g,0,sizeof(g)); 53 c=read();p=read(); 54 for(int j=1;j<=c;j++) 55 { 56 tmp=read(); 57 for(int k=1;k<=tmp;k++) 58 { 59 tmp2=read(); 60 g[j][tmp2]=1; 61 } 62 } 63 ans=hungary(); 64 if(ans==c)printf("YES "); 65 else printf("NO "); 66 } 67 return 0;