USACO 4.2 The Perfect Stall(二分图匹配匈牙利算法)

The Perfect Stall
Hal Burch

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.

PROGRAM NAME: stall4

INPUT FORMAT

Line 1: One line with 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.
Line 2..N+1: N lines, each corresponding 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.

SAMPLE INPUT (file stall4.in)

5 5
2 2 5
3 2 3 4
2 1 5
3 1 2 5
1 2 

OUTPUT FORMAT

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

SAMPLE OUTPUT (file stall4.out)

4

——————————————————————————————————
二分图匹配匈牙利算法模板
算法简述:
对于当前点x,选一个点y进行匹配,如果这个点y与另一点x'匹配,进行递归知道为x'找到另一个分配或无法找到另一分配
 1 /*
 2 ID:ivorysi
 3 PROG:stall4
 4 LANG:C++
 5 */
 6 #include <iostream>
 7 #include <cstdio>
 8 #include <cstring>
 9 #include <queue>
10 #include <set>
11 #include <vector>
12 #include <cmath>
13 #define inf 0x7fffffff
14 #define ivorysi
15 #define siji(i,x,y) for(int i=(x);i<=(y);++i)
16 #define gongzi(j,x,y) for(int j=(x);j>=(y);--j)
17 #define xiaosiji(i,x,y) for(int i=(x);i<(y);++i)
18 #define sigongzi(j,x,y) for(int j=(x);j>(y);--j)
19 #define p(x) (x)*(x)
20 using namespace std;
21 vector<int> g[505];
22 int used[505],from[505];
23 int n,m,ans;
24 bool find(int x){
25     used[x]=1;
26     for(int i=0;i<g[x].size();++i) {
27         if(from[g[x][i]]) {
28             if(used[from[g[x][i]]]==0 && find(from[g[x][i]])) {
29                 from[g[x][i]]=x;
30                 return true;
31             }
32         }
33         else {
34             from[g[x][i]]=x;
35             return true;
36         }
37     }
38     return false;
39 }
40 void init() {
41     scanf("%d%d",&n,&m);
42     int s,b;
43     siji(i,1,n) {
44         scanf("%d",&s);
45         siji(j,1,s) {
46             scanf("%d",&b);
47             g[i].push_back(b+200);
48         }
49     }
50     
51 }
52 void solve() {
53     init();
54     siji(i,1,n) {
55         memset(used,0,sizeof(used));
56         if(find(i)) ++ans;
57     }
58     printf("%d\n",ans);
59 }
60 int main(int argc, char const *argv[])
61 {
62 #ifdef ivorysi
63     freopen("stall4.in","r",stdin);
64     freopen("stall4.out","w",stdout);
65 #else
66     freopen("f1.in","r",stdin);
67 #endif
68     solve();
69     return 0;
70 }

 

 
posted @ 2017-01-16 20:35  sigongzi  阅读(578)  评论(0编辑  收藏  举报