POJ 1469 COURSES 二分图最大匹配

悠悠 2022-05-27 01:22 264阅读 0赞

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.

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

  1. 2
  2. 3 3
  3. 3 1 2 3
  4. 2 1 2
  5. 1 1
  6. 3 3
  7. 2 1 3
  8. 2 1 3
  9. 1 1

Sample Output

  1. YES
  2. NO

题意:2组数据,有3门课,3个学生,给出第i门课有j个人选择,分别是A0,A1,A2。。。求是否可以1每个学生选择一个不同的课程 2每个课程都有不同的代表 ,则把学生和课程作为二分图的两个集合,然后最大匹配==课程数的话就YES。

  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<string>
  4. #include<math.h>
  5. #include<vector>
  6. #include<queue>
  7. #include<stack>
  8. #include<map>
  9. #include<iostream>
  10. #include<algorithm>
  11. using namespace std;
  12. int n,m,ma[505][505],vis[505],dx[505];
  13. int find(int i)
  14. {
  15. for(int j=1;j<=m;j++)
  16. {
  17. if(ma[i][j]&&vis[j]==0)
  18. {
  19. vis[j]=1;
  20. if(dx[j]==-1||find(dx[j]))
  21. {
  22. dx[j]=i;
  23. return 1;
  24. }
  25. }
  26. }
  27. return 0;
  28. }
  29. int main()
  30. {
  31. int t,i,j,tt,sum,x;
  32. scanf("%d",&t);
  33. while(t--)
  34. {
  35. memset(ma,0,sizeof ma);
  36. memset(vis,0,sizeof vis);
  37. memset(dx,-1,sizeof dx);
  38. scanf("%d%d",&n,&m);
  39. for(i=1;i<=n;i++)
  40. {
  41. scanf("%d",&tt);
  42. for(j=1;j<=tt;j++)
  43. {
  44. scanf("%d",&x);
  45. ma[i][x]=1;
  46. }
  47. }
  48. sum=0;
  49. for(i=1;i<=n;i++)
  50. {
  51. memset(vis,0,sizeof vis);
  52. sum+=find(i);
  53. }
  54. //printf("%d\n",sum);
  55. if(sum==n)
  56. printf("YES\n");
  57. else
  58. printf("NO\n");
  59. }
  60. }

发表评论

表情:
评论列表 (有 0 条评论,264人围观)

还没有评论,来说两句吧...

相关阅读

    相关 [二分]匹配

    二分图的定义,以及判断图是否为二分图都很简单了。 现在要说二分图的最大匹配。 首先是定义吧,完美匹配就是一一对应,而最大匹配则是最大可以匹配的条数 完美匹配一定是最大匹配