POJ 2239 哈希取址+二分匹配

B - Selecting Courses
Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

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

这题刚开始理解题目错了,尼玛,看到样例中1 1有几个,所以还以为是重复的呢,就只取了一个,然后就剩下(1,1),(2,2),(3,2),(3,3),然后就得出样例答案为4.。直接WA一发,然后又重新读题才发现理解错题意了,晕……

题意原来是当前课程会在那些时间上几次课,刚开始因为当前是一个结点了,然后周数与节数又有两个数据,所以想想这两个如果要想放在图论中,那么当然得把这两个数据合成一个地址……哈哈这么想着就有思路了……因为以前做过哈希方面的题,所以保存了好多哈希取址的计算函数,在实际中用得最多最高效也是最容易的就是BKDRHash哈希函数了,其计算式为:a*131+b这个就作为一个新地址存下来,也就是另一个结点……因为这个计算式已经被好多人证实过了,对于全部的(a,b),其取唯一地址的函数可以用这个计算,这个函数在实际中也是证明被用得最多的了,当然这个没有处理地址冲突,所以地址还是有bug的,但是就这水题这个式子够了……

然后有了这两个结点当然就可以做边了,匹配就是二分匹配了……

哈希不懂的可以看我的另一篇博文:http://blog.csdn.net/u011466175/article/details/17484687

#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <list>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#define PI acos(-1.0)
#define mem(a,b) memset(a,b,sizeof(a))
#define sca(a) scanf("%d",&a)
#define pri(a) printf("%d\n",a)
#define lson i<<1,l,mid
#define rson i<<1|1,mid+1,r
#define MM 1000005
#define MN 100010
#define INF 55566677
#define eps 1e-7
using namespace std;
typedef long long ll;
vector<int>v[MN];
int vis[MN],pre[MN];
int dfs(int u)
{
    for(int i=0; i<v[u].size(); i++)
    {
        int j=v[u][i];
        if(!vis[j])
        {
            vis[j]=1;
            if(!pre[j]||dfs(pre[j]))
            {
                pre[j]=u;
                return 1;
            }
        }
    }
    return 0;
}
int solve(int n)
{
    int ans=0;
    for(int i=1; i<=n; i++)
    {
        mem(vis,0);
        ans+=dfs(i);
    }
    return ans;
}
int main()
{
    int n,m,i,j,p,q;
    while(~sca(n))
    {
        for(i=0;i<=n;i++) v[i].clear();
        mem(pre,0);
        for(i=1;i<=n;i++)
        {
            sca(m);
            while(m--)
            {
                scanf("%d%d",&p,&q);
                int cnt=p*131+q;//BKDRHash哈希取址计算式即可
                v[i].push_back(cnt);
            }
        }
        pri(solve(n));
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值