POJ 2239 二分图匹配/Hungary

Selecting Courses

Time Limit: 1000MS Memory Limit: 65536K

Total Submissions: 8667 Accepted: 3863

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

Source

POJ Monthly,Li Haoyuan

<span style="color:#3333ff;">/**********************************************
        author    : Grant Yuan
        time      : 2014/9/20 16:32
        source    : POJ 2239
        algorithm : Hungary
***********************************************/

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cmath>
#define MAX 310
using namespace std;
int match[MAX],mat[MAX][MAX];
bool used[MAX];
int n;

bool dfs(int k)
{
    for(int i=1;i<150;i++)
    {
       if(mat[k][i]&&!used[i])
      {
        used[i]=1;
        if(match[i]==-1||dfs(match[i]))
           {
               match[i]=k;
               return true;
           }
       }
    }
    return false;
}

int Hungary()
{
    int res=0;
     for(int i=1;i<=n;i++)
     {
         memset(used,0,sizeof(used));
         if(dfs(i)) {res++;}
     }
     return res;
}

int main()
{
    while(~scanf("%d",&n)){
        memset(match,-1,sizeof(match));
        memset(used,0,sizeof(used));
        memset(mat,0,sizeof(mat));
        for(int i=1;i<=n;i++)
        {
            int m;
            scanf("%d",&m);
            for(int j=1;j<=m;j++)
            {
                int k1,k2;
                scanf("%d%d",&k1,&k2);
                mat[i][12*(k1-1)+k2]=1;
            }
        }
        int ans=Hungary();
        printf("%d\n",ans);
    }
    return 0;
}
</span>
时间: 2024-12-30 13:56:22

POJ 2239 二分图匹配/Hungary的相关文章

poj 2239(二分图匹配)

题意:有n门课,每门课都有一些上课时间,周几的第几节课,一个人想报尽量多的课但时间不能冲突,问最多能报多少门课. 题解:只需要把课程当做一个集合,上课时间当做另一个集合,然后把每个课程和它的上课时间(时间可以用(p - 1) * 12 + p表示)都加一条边到图里,找最大匹配数让每个课程至少对应一个上课时间. #include <stdio.h> #include <string.h> #include <vector> using namespace std; con

POJ 2446 二分图匹配

题意:给你一个n*m方格 让你用1*2的的小方格去铺满,其中有k个方格不能被铺到. 思路:二分图建图, 以每个格子为点建图,如果可以用一块1*2的小方格铺到,就连一条边. 每个格子在X集合和Y集合都有一个点,只要任意一边被匹配到了就算可以,然后就是二分图匹配了. 上代码. #include<stdio.h> #include<string.h> #include<algorithm> #include<iostream> #include<vector

POJ 1325 二分图匹配/匈牙利算法

Machine Schedule Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 11922 Accepted: 5077 Description As we all know, machine scheduling is a very classical problem in computer science and has been studied for a very long history. Scheduling p

poj 1325(二分图匹配)

题意:有两台机器有各有n和m种模式(都是从0开始编号),有k个任务,每个任务可以是第一台机器的x模式或第二台机器的y模式,每台机器切换模式都要重启(重启后初始模式是0),问最少次数重启使所有任务完成. 题解:把任务当做边,两台机器的模式放在不同集合中,那么就是二分图的最小点集覆盖问题,注意模式0可以不计算,因为重启初始模式就是0,不需要花费重启次数. #include <stdio.h> #include <string.h> const int N = 105; int n, m

POJ 3041 -- 二分图匹配

题意:有个N*N的网格,有一部分格子里有陨石,小明有很牛逼的武器,打一枪后一行或一列的陨石就没了,给出陨石分布,求最小打炮数. 分析:其实就是Konig定理.记最小打炮数为m,在网格里你最多可以找出M个互相之间既不在同一行又不在同一列的元素,也就是说你再找一个元素它必定与已有的某个元素同行或同列,Konig定理就是:m = M.至于具体打时是打某一行还是某一列,要看剩下的元素与现有元素共行或共列的情况了. 那么问题就转化成找出这M个陨石,把行和列看做一个二分图,那么找M就是找最大匹配了. 和PO

Antenna Placement POJ - 3020 二分图匹配 匈牙利 拆点建图 最小路径覆盖

题意:图没什么用  给出一个地图 地图上有 点 一次可以覆盖2个连续 的点( 左右 或者 上下表示连续)问最少几条边可以使得每个点都被覆盖 最小路径覆盖       最小路径覆盖=|G|-最大匹配数                   证明:https://blog.csdn.net/qq_34564984/article/details/52778763 证明总的来说就是尽可能多得连边 边越多 可以打包一起处理得点就越多(这里题中打包指连续得两个点只需要一条线段就能覆盖) 拆点思想   :匈牙

POJ 3020 Antenna Placement(二分图匹配)

POJ 3020 Antenna Placement 题目链接 题意:给定一个地图,'*'的地方要被覆盖上,每次可以用1 x 2的矩形去覆盖,问最少用几个能覆盖 思路:二分图匹配求最大独立集,相邻*之间连边,然后求最大独立集即可 看这数据范围,用轮廓线dp应该也能搞 代码: #include <cstdio> #include <cstring> #include <vector> #include <algorithm> using namespace s

POJ - 1904 King&#39;s Quest(强连通分量+二分图匹配)

题目大意:有N个帅哥和N个美女,现在给出每个帅哥所喜欢的美女的编号,和一个帅哥和美女的完美匹配 问每个帅哥可以娶多少个美女,且当他娶完这个美女后,剩下的人还可以完美匹配 解题思路:神题啊,给一个大神的详细解答 具体是这样的,首先先建边,把帅哥和能娶到的美女连边,再把完美匹配的美女和帅哥连边,这样就形成了一张有向图了 接着,找出这张有向图的所有强连通分量,在强连通分量里面的帅哥都可以娶到自己喜欢的美女,且娶完这个美女后,不会影响到其他人 为什么呢? 假设xi为帅哥,yi和yj为美女,假设给定的完美

二分图匹配 最大匹配数+最大点覆盖 POJ 1469+POJ 3041

最大匹配数就等于最大点覆盖,因为在图里面,凡是要覆盖的点必定是连通的,而最大匹配之后,若还有点没有覆盖到,则必定有新的匹配,与最大匹配数矛盾,如果去掉一些匹配,则必定有点没有覆盖到. POJ 1469 比较简单,用的经典的二分图匹配算法. ? 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