POJ_1274_二分匹配题解

The Perfect Stall

POJ - 1274

时限: 1000MS 内存: 10000KB    64位IO格式: %I64d & %I64u

问题描述

Farmer John completed his new barn just last week, complete with all the latest milking technology. Unfortunately, due to engineering problems, all the stalls in the new barn are different. For the first week, Farmer John randomly assigned cows to stalls, but it quickly became clear that any given cow was only willing to produce milk in certain stalls. For the last week, Farmer John has been collecting data on which cows are willing to produce milk in which stalls. A stall may be only assigned to one cow, and, of course, a cow may be only assigned to one stall.

Given the preferences of the cows, compute the maximum number of milk-producing assignments of cows to stalls that is possible.

输入

The input includes several cases. For each case, the first line contains two integers, N (0 <= N <= 200) and M (0 <= M <= 200). N is the number of cows that Farmer John has and M is the number of stalls in the new barn. Each of the following N lines corresponds to a single cow. The first integer (Si) on the line is the number of stalls that the cow is willing to produce milk in (0 <= Si <= M). The subsequent Si integers on that line are the stalls in which that cow is willing to produce milk. The stall numbers will be integers in the range (1..M), and no stall will be listed twice for a given cow.

输出

For each case, output a single line with a single integer, the maximum number of milk-producing stall assignments that can be made.

参考代码:

 1 //这道题是二分匹配 匈牙利算法 套模板
 2
 3 #include <cstdio>
 4 #include <cstring>
 5 #include <algorithm>
 6 #include <iostream>
 7
 8 using namespace std;
 9
10 const int M=200+5;
11 int n,m;
12 int link[M];     //最终关系
13 bool MAP[M][M];  //关系图
14 bool cover[M];  //位置是否被占用
15 int ans;
16
17 void init()
18 {
19     int num;
20     int y;
21     memset(MAP,false,sizeof(MAP));
22     for(int i=1;i<=n;i++)
23     {
24         scanf("%d",&num);
25         while(num--)
26         {
27             scanf("%d",&y);
28             MAP[i][y]=true;
29         }
30     }
31 }
32
33 bool dfs(int x)
34 {
35     for(int  y=1;y<=m;y++)
36     {
37         if(MAP[x][y]&&!cover[y])
38         {
39             cover[y]=true;
40             if(link[y]==-1||dfs(link[y]))
41             {
42                 link[y]=x;
43                 return true;
44             }
45         }
46     }
47     return false;
48 }
49 int main()
50 {
51     while(scanf("%d%d",&n,&m)!=EOF)
52     {
53         ans=0;
54         init();
55         memset(link,-1,sizeof(link));
56         for(int i=1;i<=n;i++)
57         {
58             memset(cover,false,sizeof(cover));
59             if(dfs(i))
60             {
61                 ans++;
62             }
63         }
64         printf("%d\n",ans);
65     }
66     return 0;
67 }

原文链接:http://my.oschina.net/zqmath1994/blog/538627

时间: 2024-10-09 15:05:53

POJ_1274_二分匹配题解的相关文章

Card Game Cheater(贪心+二分匹配)

Card Game Cheater Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 1566    Accepted Submission(s): 822 Problem Description Adam and Eve play a card game using a regular deck of 52 cards. The rule

hdu_5727_Necklace(二分匹配)

题目连接:hdu_5727_Necklace 题意: 有2*n个珠子,n个阳珠子,n个阴珠子,现在要将这2n个珠子做成一个项链,珠子只能阴阳交替排,有些阳珠子周围如果放了指定的阴珠子就会变坏,给你一个n和m个关系x y,这些关系指明了阳珠子x周围放y阴珠子会变坏,现在问你做成这条项链,最少变坏的阳珠子有多少个 题解: 官方题解给的是用DFS 带剪枝来做,不过我感觉那样好玄学,我的做法是将所有的阴珠子可能组成的组合全部算出来,然后用阳珠子去插空,这里插空我们要找最大的匹配,也就是我们可以用二分匹配

hdu 1054 Strategic Game (二分匹配)

Strategic Game Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4697    Accepted Submission(s): 2125 Problem Description Bob enjoys playing computer games, especially strategic games, but somet

BZOJ 1191: [HNOI2006]超级英雄Hero 二分匹配

1191: [HNOI2006]超级英雄Hero Description 现在电视台有一种节目叫做超级英雄,大概的流程就是每位选手到台上回答主持人的几个问题,然后根据回答问题的多少获得不同数目的奖品或奖金.主持人问题准备了若干道题目,只有当选手正确回答一道题后,才能进入下一题,否则就被淘汰.为了增加节目的趣味性并适当降低难度,主持人总提供给选手几个“锦囊妙计”,比如求助现场观众,或者去掉若干个错误答案(选择题)等等. 这里,我们把规则稍微改变一下.假设主持人总共有m道题,选手有n种不同的“锦囊妙

HDU 3861 The King’s Problem (强连通+二分匹配)

题目地址:HDU 3861 这题虽然是两个算法结合起来的.但是感觉挺没意思的..结合的一点也不自然,,硬生生的揉在了一块...(出题者不要喷我QAQ.) 不过这题让我发现了我的二分匹配已经好长时间没用过了..都快忘了..正好在省赛之前又复习了一下. 代码如下: #include <iostream> #include <string.h> #include <math.h> #include <queue> #include <algorithm>

hdu 4185 Oil Skimming(二分匹配)

Oil Skimming Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 883    Accepted Submission(s): 374 Problem Description Thanks to a certain "green" resources company, there is a new profitable

【DFS求树的最大二分匹配+输入外挂】HDU 6178 Monkeys

http://acm.hdu.edu.cn/showproblem.php?pid=6178 [题意] 给定一棵有n个结点的树,现在有k个猴子分布在k个结点上,我们可以删去树上的一些边,使得k个猴子每个猴子都至少和其他一个猴子相连 问树上最少保留多少条边 [思路] 每个猴子要至少和一个猴子相连,考虑保留的边最少,那么最优的情况一定是一条边的两个顶点放两个猴子,这些边的顶点都不重合 我们现在要找到给定的树中最多有多少条这样的边,即最大二分匹配 O(n)的DFS,对于每个结点,优先与叶子结点形成一条

hdu 5093 Battle ships 最大二分匹配

Battle ships Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 589    Accepted Submission(s): 233 Problem Description Dear contestant, now you are an excellent navy commander, who is responsible

二分匹配最大匹配 PKU1469

一个学生可以有多种选择,问能否每个学生刚好选一门课,但是每门课最多只有一个学生可以选择 典型的二分匹配最大匹配,直接套模板 COURSES Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 17878   Accepted: 7048 Description Consider a group of N students and P courses. Each student visits zero, one or more