#POJ 2239 Selecting Courses (匈牙利算法 / 二分图的最大匹配)

Description

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.

Sample Input

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

Sample Output

4

 题目大意 : 给出课程的数目,然后给出每节课可以选择的上课时间 (周X的第Y节课),求最多可以选择多少课。

思路 : 如果匈牙利算法理解了的话这道题和模板题没什么两样。我把这个算法就当成了  “连线” 问题, 模板给的图一般都是一维连一维的,而这道题就是个一维连二维的,一维 : 课程的下标,也就是相当于数学课啊、语文课啊这样只有一个元素的。二维 :星期几和第几节课,这就由两个部分组成了。所以我们只要将模板上的算法稍微改动一下,就是这道题的答案了!具体匈牙利算法怎么实现的,网上大佬们画的图一个比一个清楚哈,我就不解释了。一句话概括 : 如果二者之间有线可连,那么就连上,如果没有,则跳过,如果已经被其他点连上了,试试看能不能把那个点换一条线连,而这布,就是一个递归的过程(其实不止一句话了。。)

AC代码 :

#include<iostream>
#include<cstring>
using namespace std;
const int maxn = 3e2 + 5;

int p[maxn][15][15], net[15][15], n, t;
bool vis[15][15];
bool find_(int x) {
    for (int i = 1; i <= 7; i++) {
        for (int j = 1; j <= 12; j++) {
            if (!vis[i][j] && p[x][i][j]) {
                vis[i][j] = 1;
                if (!net[i][j] || find_(net[i][j])) {
                    net[i][j] = x;
                    return true;
                }
            }
        }
    }
    return false;
}

int main()
{
    while (cin >> n) {
        memset(p, 0, sizeof(p));
        memset(net, 0, sizeof(net));
        for (int i = 1; i <= n; i++) {
            cin >>t;
            while (t--) {
                int ai, bi;
                cin >> ai >> bi;
                p[i][ai][bi] = 1;
            }
        }
        int ans = 0;
        for (int i = 1; i <= n; i++) {
            memset(vis, 0, sizeof(vis));
            if (find_(i)) ans++;
        }
        cout << ans << endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_43851525/article/details/91373918