poj 1274 The Prefect Stall

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 22736   Accepted: 10144

Description

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.

Input

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.

Output

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

Sample Input

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

Sample Output

 4

Source


  这道题没有什么特别好说的,直接匈牙利算法不解释

Code:

 1 /**
 2  * poj.org
 3  * Problem#1274
 4  * Accepted
 5  * Time:16ms
 6  * Memory:520k/540k
 7  */
 8 #include<iostream>
 9 #include<queue>
10 #include<set>
11 #include<map>
12 #include<cctype>
13 #include<algorithm>
14 #include<cstring>
15 #include<cstdlib>
16 #include<stdarg.h>
17 #include<fstream>
18 #include<ctime>
19 using namespace std;
20 typedef bool boolean;
21 typedef class Edge {
22     public:
23         int end;
24         int next;
25         Edge():end(0),next(0){}
26         Edge(int end, int next):end(end),next(next){}
27 }Edge;
28 int *h;
29 int _count = 0;
30 Edge* edge;
31 inline void addEdge(int from,int end){
32     edge[++_count] = Edge(end,h[from]);
33     h[from] = _count; 
34 }
35 int result;
36 int *match;
37 boolean *visited;
38 boolean find(int node){
39     for(int i = h[node];i != 0;i = edge[i].next){
40         if(visited[edge[i].end]) continue;
41         visited[edge[i].end] = true;
42         if(match[edge[i].end] == -1||find(match[edge[i].end])){
43             match[edge[i].end] = node;
44             return true;
45         }
46     }
47     return false;
48 }
49 int n,m;
50 void solve(){
51     for(int i = 1;i <= n;i++){
52         if(match[i] != -1)    continue;
53         memset(visited, false, sizeof(boolean) * (n + m + 1));
54         if(find(i)) result++;
55     }
56 }
57 int buf;
58 int b;
59 boolean init(){
60     if(~scanf("%d%d",&n,&m)){
61         result = 0;
62         visited = new boolean[(const int)(n + m + 1)];
63         match = new int[(const int)(n + m + 1)];
64         edge = new Edge[(const int)((n * m) + 1)];
65         h = new int[(const int)(n + m + 1)];
66         memset(h, 0, sizeof(int)*(n + m + 1));
67         memset(match, -1,sizeof(int)*(n + m + 1));
68         for(int i = 0;i ^ n;i++){
69             scanf("%d",&buf);
70             for(int j = 0;j ^ buf;j++){
71                 scanf("%d",&b);
72                 addEdge(i + 1, b + n);
73         //        addEdge(b + n, i + 1);
74             }
75         }
76         return true;
77     }
78     return false;
79 }
80 void freeMyPoint(){
81     delete[] visited;
82     delete[] match;
83     delete[] edge;
84     delete[] h;
85 }
86 int main(){
87     while(init()){
88         solve();
89         printf("%d
",result);
90         freeMyPoint();
91     }
92     return 0;
93 }
原文地址:https://www.cnblogs.com/yyf0309/p/5682724.html