首页 > 代码库 > poj 1469 COURSES 题解

poj 1469 COURSES 题解

COURSES
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 21515 Accepted: 8455

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

23 33 1 2 32 1 21 13 32 1 32 1 31 1

Sample Output

YESNO

Source

Southeastern Europe 2000
——————————————————————————我是分割线——————————————————————————————

二分图最大匹配。

邻接矩阵map[i][j]表示j号喜欢i号课程,然后对课程进行寻找匹配,匹配成功则标记,之后统计匹配数目即可。

这题丧心病狂卡时,我改了两天,最后发现I/O超时了。。。 取消同步后的流还是略微慢一些,用标准输入输出可以正好卡过。TAT

技术分享
 1 /* 2     Problem: 3     OJ:      4     User:    5     Time:    6     Memory:   7     Length:   8 */ 9 #include<iostream>10 #include<cstdio>11 #include<cstring>12 #include<cmath>13 #include<algorithm>14 #include<queue>15 #include<cstdlib>16 #include<iomanip>17 #include<cassert>18 #include<climits>19 #include<vector>20 #include<list>21 #include<map>22 #define maxn 100123 #define F(i,j,k) for(int i=j;i<=k;i++)24 #define M(a,b) memset(a,b,sizeof(a))25 #define FF(i,j,k) for(int i=j;i>=k;i--)26 #define inf 0x7fffffff27 #define maxm 201628 #define mod 100000000729 //#define LOCAL30 using namespace std;31 int read(){32     int x=0,f=1;char ch=getchar();33     while(ch<0||ch>9){if(ch==-)f=-1;ch=getchar();}34     while(ch>=0&&ch<=9){x=x*10+ch-0;ch=getchar();}35     return x*f;36 }37 int n,m,p;38 bool kc[maxn][maxn];39 bool vis[maxn];40 int pp[maxn];41 inline int path(int u)42 {43     int a,b,temp;44     F(i,1,n){45         if(kc[u][i]&&!vis[i]){46             vis[i]=true;47             temp=pp[i];48             pp[i]=u;49             if(temp==-1||path(temp)) return 1;50             pp[i]=temp;51         }52     }53     return 0;54 }55 inline int solve()56 {57     int a,b,ans=0;58     M(pp,-1);59     F(i,1,p){60         M(vis,0);61 //        ans+=path(i);62 if(path(i)) ans++;63         if(ans==p) break;64     }65     return ans;66 }67 int main()68 {69     int t;cin>>t;70     while(t--){71         scanf("%d", &p);scanf("%d", &n);72         M(kc,0);73         int num,cnt;74         F(i,1,p){75             scanf("%d", &num);76             F(j,1,num){77                 scanf("%d", &cnt);78                 kc[i][cnt]=true;79             }80         }81         if(solve()==p) printf("YES\n");82         else printf("NO\n");83     }84     return 0;85 }
View Code

 

poj 1469 COURSES 题解