POJ2239 Selecting Courses

题目

Description

1000MS/65536K
It is well known that it is not easy to select courses in the college, for there is usually conflict among the time of the courses. Li Ming is a student who loves study every much, and at the beginning of each term, he always wants to select courses as more as possible. Of course there should be no conflict among the courses he selects.

There are 12 classes every day, and 7 days every week. There are hundreds of courses in the college, and teaching a course needs one class each week. To give students more convenience, though teaching a course needs only one class, a course will be taught several times in a week. For example, a course may be taught both at the 7-th class on Tuesday and 12-th class on Wednesday, you should assume that there is no difference between the two classes, and that students can select any class to go. At the different weeks, a student can even go to different class as his wish. Because there are so many courses in the college, selecting courses is not an easy job for Li Ming. As his good friends, can you help him?

Input

The input contains several cases. For each case, the first line contains an integer n (1 <= n <= 300), the number of courses in Li Ming’s college. The following n lines represent n different courses. In each line, the first number is an integer t (1 <= t <= 7*12), the different time when students can go to study the course. Then come t pairs of integers p (1 <= p <= 7) and q (1 <= q <= 12), which mean that the course will be taught at the q-th class on the p-th day of a week.

Output

For each test case, output one integer, which is the maximum number of courses Li Ming can select.

Simple input

5
1 1 1
2 1 1 2 2
1 2 2
2 3 2 3 3
1 3 3

Simple output

4

题目分析

这道题的大意就是学校里会开n门课,每门课一周可以上多节内容是一致的,学校的安排是每周7天每天十二节课,问最多可以选多少门课程。其实这就是一到二分图的题,问题在于如何建图,可以将每周的课程安排放入一个7*12的矩阵里面,并给每一个点赋值建立课程安排与时间的关系图,然后求该图最大匹配即可.

AC代码

8596K/63MS

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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <ctype.h>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define eps 1e-8
#define INF 0x7fffffff
#define N 1005
#define PI acos(-1.0)
#define seed 31//131,1313
typedef long long LL;
typedef unsigned long long ULL;
using namespace std;
int useif[N]; //记录y中节点是否使用 0表示没有访问过,1为访问过
int link[N]; //记录当前与y节点相连的x的节点
int mat[N][N]; //记录连接x和y的边,如果i和j之间有边则为1,否则为0
int gn,gm; //二分图中x和y中点的数目
//char mapp[N][N];
int mat1[N][N];
int can(int t)
{

int i;
for(i=1; i<=gm; i++)
{
if(useif[i]==0 && mat[t][i])
{
useif[i]=1;
if(link[i]==-1 || can(link[i]))
{
link[i]=t;
return 1;
}
}
}
return 0;
}
int MaxMatch()
{

int i,num;
num=0;
memset(link,0xff,sizeof(link));
for(i=1; i<=gn; i++)
{
memset(useif,0,sizeof(useif));
if(can(i))
num++;
}
return num;
}
void init()
{

memset(useif,0,sizeof(useif));
memset(link,0,sizeof(link));
memset(mat,0,sizeof(mat));
memset(mat1,0,sizeof(mat1));
}
int main()
{


int t;
while(scanf("%d",&t)!=EOF)
{
int n,m,num=1;
init();
for(int i=1; i<=7; i++)
{
for(int j=1; j<=12; j++)
{
mat1[i][j]=num++;
}
}
for(int i=1; i<=t; i++)
{
int tem;
scanf("%d",&tem);
for(int j=1; j<=tem; j++)
{
int a,b;
scanf("%d%d",&a,&b);
mat[i][mat1[a][b]]=1;
}
}
gm=7*12;
gn=t;
int ans=MaxMatch();
printf("%d\n",ans);
}
return 0;
}

题目链接

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