HDU-----(1083)Courses(最大匹配)

Courses

Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3767    Accepted Submission(s): 1800

Problem 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

Your
program should read sets of data from a text file. The first line of
the input file 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‘ll 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.

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.

An example of program input and output:

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

Southeastern Europe 2000

给定一个课程数目和每一个门课程听课的人的编号,问能否每一门课都有人听课...!

代码:

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<vector>
 4 using namespace std;
 5 const int maxn=301;
 6 vector<int>mat[maxn];
 7 int judge[maxn];
 8 bool vis[maxn];
 9 bool skm(int x){
10   vector<int>::iterator it;
11   for(it=mat[x].begin();it<mat[x].end();it++)
12     {
13       if(!vis[*it])
14       {
15         vis[*it]=1;
16      if(judge[*it]==0||skm(judge[*it]))
17      {
18          judge[*it]=x;
19          return 1;
20      }
21     }
22   }
23   return 0;
24 }
25
26 int main()
27 {
28     int cas,n,m,t,a,i;
29     scanf("%d",&cas);
30     while(cas--)
31     {
32      scanf("%d%d",&n,&m);
33      memset(judge,0,sizeof(int)*(m+2));
34     for( i=1;i<=n;i++)
35     {
36       mat[i].clear();
37       scanf("%d",&t);
38      while(t--){
39          scanf("%d",&a);
40         mat[i].push_back(a);
41      }
42     }
43     for(i=1;i<=n;i++){
44       memset(vis,0,sizeof(bool)*(m+2));
45       if(!skm(i))break;
46     }
47     if(i>n) printf("YES\n");
48     else printf("NO\n");
49     }
50  return 0;
51 }

采用邻接矩阵做:

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<vector>
 4 #include<cstdlib>
 5 #include<iostream>
 6 using namespace std;
 7 const int maxn=505;
 8 bool mat[maxn][maxn];
 9 int judge[maxn];
10 bool vis[maxn];
11 int n,m;
12 bool skm(int x){
13   for(int i=1;i<=m;i++)
14   {
15       if(!vis[i]&&mat[x][i])
16       {
17         vis[i]=1;
18      if(judge[i]==0||skm(judge[i]))
19      {
20          judge[i]=x;
21          return 1;
22      }
23     }
24   }
25   return 0;
26 }
27 int main()
28 {
29     int cas,t,a,i;
30     //freopen("test.in","r",stdin);
31     scanf("%d",&cas);
32     while(cas--)
33     {
34      scanf("%d%d",&n,&m);
35      memset(judge,0,sizeof(judge));
36      memset(mat,0,sizeof(mat));
37     for( i=1;i<=n;i++)
38     {
39       scanf("%d",&t);
40      while(t--)
41      {
42          scanf("%d",&a);
43          mat[i][a]=1;
44      }
45     }
46     for(i=1;i<=n;i++){
47       memset(vis,0,sizeof(vis));
48       if(!skm(i))break;
49     }
50     if(i>n)
51         printf("YES\n");
52     else
53         printf("NO\n");
54     }
55  return 0;
56 }

时间: 2024-11-05 13:59:43

HDU-----(1083)Courses(最大匹配)的相关文章

Hdu 1083 Courses(匈牙利算法模版题)

Hdu 1083 题意:给你一个p表示测试组数,给你n和m表示课的个数和学生的个数,接下来n行首数字i表示该堂课的学生代表人数,之后为i个学生编码,问能否为每堂课找到一个学生课代表且不冲突: 题解:匈牙利算法模版 另附简单易懂匈牙利算法讲解:传送门 #include<cstring> #include<cstdio> const int N =305; using namespace std; bool h[N][N]; bool vis[N]; int link[N]; int

HDU 1083 Courses(最大匹配模版题)

题目大意: 一共有N个学生跟P门课程,一个学生可以任意选一 门或多门课,问是否达成: 1.每个学生选的都是不同的课(即不能有两个学生选同一门课) 2.每门课都有一个代表(即P门课都被成功选过) 输入为: 第一行一个T代表T组数据 P N(P课程数, N学生数) 接着P行: 第几行代表第几门课程,首先是一个数字k代表对这门课程感兴趣的同学的个数,接下来是k个对这门课程感兴趣同学的编号. 输出为: 若能满足上面两个要求这输出”YES”,否则为”NO” 注意:是课程匹配的学生,学生没课上没事.....

hdu 1083 Courses(二分图匹配)

Courses Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 4278    Accepted Submission(s): 2036 Problem Description Consider a group of N students and P courses. Each student visits zero, one or

hdu 1083 Courses

描述:有p门的课,每门课都有若干学生,现在要为每个课程分配一名课代表,每个学生只能担任一门课的课代表,如果每个课都能找到课代表,则输出"YES",否则"NO". 二分匹配 #include<iostream> #include<cstring> #define maxn 305 using namespace std; int c,s; int rem[maxn][maxn]; int visit[maxn],map[maxn]; int d

HDU - 1083 Courses (二分图最大匹配模板)

题意:给出P门课程,N个学生.每一门课程可能有多个学生感兴趣然后我们需要匹配,使得每一门课程都只包含一名对其感兴趣的学生问:能否匹配成立思路:这个就是典型的二分图匹配问题.常用匈牙利算法 完整代码:(一开始写成了无向图....)写成有向图是因为学生是可以剩余的 #include <cstdio> #include <iostream> #include <algorithm> #include <cstring> using namespace std; c

hdu 1083 最大匹配

题意:N个学生 P 个课程  求最大匹配 3 3 //学生 课程 3 1 2 3 //课程1 匹配学生1 2 3 2 1 2 1 1典型的匹配没什么好说的 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 #include<iostream

HDU 1083 裸的二分匹配

Courses Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 3424    Accepted Submission(s): 1650 Problem Description Consider a group of N students and P courses. Each student visits zero, one or

HDU 1528 (二分图最大匹配 + 最小覆盖, 14.07.17)

Problem Description Adam and Eve play a card game using a regular deck of 52 cards. The rules are simple. The players sit on opposite sides of a table, facing each other. Each player gets k cards from the deck and, after looking at them, places the c

HDU ACM 1083 Courses 二分图最大匹配

题意:p门课,每门课有若干学生,要为每门课分配一名课代表,每个学生只能担任一门课的课代表,若每个课都能找到课代表,则输出"YES",否则"NO". 分析:二分图的最大匹配,对课程.学生关系建立一个图,进行二分图最大匹配,当最大匹配数==课程数时说明能够满足要求,否则不能. #include<iostream> using namespace std; #define N 303 bool cs[N][N]; //cs[i][j]表示学生j是否选i这个课程

C - Courses - hdu 1083(模板)

一共有N个学生跟P门课程,一个学生可以任意选一 门或多门课,问是否达成: 1.每个学生选的都是不同的课(即不能有两个学生选同一门课) 2.每门课都有一个代表(即P门课都被成功选过) 输入为: P N(课程数跟学生数) 接着有P行,格式为Count studenti studenti+1 ……studentcount (Count表示对课程1感兴趣的学生数,接着有Count个学生) 如第一行2 1 2表示学生1跟学生2对课程1感兴趣 输出为: 若能满足上面两个要求这输出”YES”,否则为”NO”