POJ——T 1469 COURSES

http://poj.org/problem?id=1469

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 24197   Accepted: 9428

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

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 <algorithm>
 2 #include <cstring>
 3 #include <cstdio>
 4 #include <vector>
 5 
 6 using namespace std;
 7 
 8 const int N(53300);
 9 int p,n,ans,match[N];
10 vector<int>vec[N];
11 bool vis[N];
12 
13 bool DFS(int now)
14 {
15     for(int v,i=0;i<vec[now].size();i++)
16     {
17         v=vec[now][i];
18         if(vis[v]) continue;
19         vis[v]=1;
20         if(!match[v]||DFS(match[v]))
21         {
22             match[v]=now;
23             return true;
24         }
25     }
26     return false;
27 }
28 
29 int AC()
30 {
31     int t; scanf("%d",&t);
32     for(;t--;ans=0)
33     {
34         for(int i=1;i<=N;i++) vec[i].clear();
35         memset(match,0,sizeof(match));
36         scanf("%d%d",&p,&n);
37         for(int q,v,u=1;u<=p;u++)
38         {
39             for(scanf("%d",&q);q--;)
40                 scanf("%d",&v),vec[u].push_back(v+p);
41         }
42         for(int i=1;i<=p;i++)
43         {
44             memset(vis,0,sizeof(vis));
45             if(DFS(i)) ans++;
46         }
47         if(ans==p) puts("YES");
48         else puts("NO");
49     }
50     return 0;
51 }
52 
53 int I_want_AC=AC();
54 int main(){;}
vector实现
 1 #include <algorithm>
 2 #include <cstring>
 3 #include <cstdio>
 4 
 5 using namespace std;
 6 
 7 const int N(55330);
 8 int match[N],head[N];
 9 int p,n,ans,sumedge;
10 struct Edge
11 {
12     int v,next;
13     Edge(int v=0,int next=0):v(v),next(next){}
14 }edge[N];
15 inline void ins(int u,int v)
16 {
17     edge[++sumedge]=Edge(v,head[u]);
18     head[u]=sumedge;
19 }
20 bool vis[N];
21 
22 bool DFS(int now)
23 {
24     for(int v,i=head[now];i;i=edge[i].next)
25     {
26         v=edge[i].v;
27         if(vis[v]) continue;
28         vis[v]=1;
29         if(!match[v]||DFS(match[v]))
30         {
31             match[v]=now;
32             return true;
33         }
34     }
35     return false;
36 }
37 
38 int AC()
39 {
40     int t; scanf("%d",&t);
41     for(;t--;sumedge=ans=0)
42     {
43         scanf("%d%d",&p,&n);
44         for(int q,v,u=1;u<=p;u++)
45         {
46             for(scanf("%d",&q);q--;)
47                 scanf("%d",&v),ins(u,v+p);
48         }
49         for(int i=1;i<=p;i++)
50         {
51             memset(vis,0,sizeof(vis));
52             if(DFS(i)) ans++;
53         }
54         if(ans==p) puts("YES");
55         else puts("NO");
56         memset(edge,0,sizeof(edge));
57         memset(head,0,sizeof(head));
58         memset(match,0,sizeof(match));
59     }
60     return 0;
61 }
62 
63 int I_want_AC=AC();
64 int main(){;}
人工 邻接表实现
——每当你想要放弃的时候,就想想是为了什么才一路坚持到现在。
原文地址:https://www.cnblogs.com/Shy-key/p/7423924.html