标签:
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
标签:
原文地址:http://www.cnblogs.com/webary/p/5018036.html