【二分图】poj 2239 Selecting Courses

来源:互联网 发布:自适应网址导航源码 编辑:程序博客网 时间:2024/06/09 17:46
Selecting Courses
Time Limit: 1000MS
Memory Limit: 65536KTotal Submissions: 10694
Accepted: 4831

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

51 1 12 1 1 2 21 2 22 3 2 3 31 3 3

Sample Output

4

题目大意:

现在有N门课程,从1到N。然后一个学生爱学习,想学习尽可能多门的课程,每门课程在一星期内可以开设多次,

但不同课程之间可能有冲突,上课时间为:每周7天,一天12节课,

现在给你每门课程的上课时间,求这个学生一周之内最多上几门课程。

解题思路:

可以转换为二分图最大匹配。一个集合为课程,一个集合为上课时间。只需要满足一门课程有且只有一个上课时间就OK了。

关键在于建图,原来想用结构体数组建图,

后来发现可能把时间转变为一个数字,就是上课天数和节数转换成 (day-1) * 12 + time就知道是第几天第几节了

///AC代码
#include <algorithm>#include <cmath>#include <cstdio>#include <cstring>#include <ctime>#include <iostream>#include <map>#include <queue>#include <set>#include <stack>#include <string>#include <vector>#define eps 1e-8#define INF 0x7fffffff#define maxn 100005#define PI acos(-1.0)using namespace std;typedef long long LL;const int N = 302;/*变种1:二分图的最小顶点覆盖:假如选了一个点就相当于覆盖了以它为端点的所有边,你需要选择最少的点来覆盖所有的边二分图的最小顶点覆盖数 = 二分图的最大匹配数变种2:DAG图(无回路有向图)的最小路径覆盖路径覆盖就是在图中找一些路经,使之覆盖了图中的所有顶点,且任何一个顶点有且只有一条路径与之关联,如果把这些路径中的每条路径从它的起始点走到它的终点,那么恰好可以经过图中的每个顶点一次且仅一次DAG图的最小路径覆盖数 = 节点数(n)- 最大匹配数(m)变种3: 二分图的最大独立集:在图中选取最多的点,使任意所选两点均不相连二分图的最大独立集数 = 节点数(n)- 最大匹配数(m)*//*=***************************************************二分图匹配(匈牙利算法的DFS实现)INIT:g[][]两边定点划分的情况CALL:res=hungary();输出最大匹配数优点:适于稠密图,DFS找增广路快,实现简洁易于理解时间复杂度:O(VE);***************************************************=*/const int MAXN = 1000;int uN, vN; //u,v数目int g[MAXN][MAXN];//编号是0~n-1的int linker[MAXN];bool used[MAXN];bool dfs(int u){    int v;    for (v = 1; v <= vN; v++)        if (g[u][v] && !used[v])        {            used[v] = true;            if (linker[v] == -1 || dfs(linker[v]))            {                linker[v] = u;                return true;            }        }    return false;}int hungary(){    int res = 0;    int u;    memset(linker, -1, sizeof(linker));    for (u = 1; u <= uN; u++)    {        memset(used, 0, sizeof(used));        if (dfs(u))        {            res++;        }    }    return res;}int main(){    int n;    while (~scanf("%d", &n))    {        uN = n;        vN = 84;        memset(g, 0, sizeof(g));        for (int i = 1; i <= n; i++)        {            int temp, day, x;            scanf("%d", &temp);            while (temp--)            {                scanf("%d%d", &day, &x);                g[i][(day - 1) * 12 + x] = 1;            }        }        int ans = hungary();        printf("%d\n", ans);    }    return 0;}

阅读全文
0 0