首页 > 代码库 > POJ 1469 COURSES
POJ 1469 COURSES
COURSES
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 17154 | Accepted: 6741 |
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
23 33 1 2 32 1 21 13 32 1 32 1 31 1
Sample Output
YESNO
Source
Southeastern Europe 2000
解题:裸的最大匹配算法题,p种课程,n个学生,每个学生可以选修多门或者不选课程。问最能否选出p个学生,使其上不同的课。匈牙利算法。
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 long13 #define INF 0x3f3f3f3f14 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 }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。