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'找到另一个分配或无法找到另一分配
/*
ID:ivorysi
PROG:stall4
LANG:C++
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <set>
#include <vector>
#include <cmath>
#define inf 0x7fffffff
#define ivorysi
#define siji(i,x,y) for(int i=(x);i<=(y);++i)
#define gongzi(j,x,y) for(int j=(x);j>=(y);--j)
#define xiaosiji(i,x,y) for(int i=(x);i<(y);++i)
#define sigongzi(j,x,y) for(int j=(x);j>(y);--j)
#define p(x) (x)*(x)
using namespace std;
vector<int> g[];
int used[],from[];
int n,m,ans;
bool find(int x){
used[x]=;
for(int i=;i<g[x].size();++i) {
if(from[g[x][i]]) {
if(used[from[g[x][i]]]== && find(from[g[x][i]])) {
from[g[x][i]]=x;
return true;
}
}
else {
from[g[x][i]]=x;
return true;
}
}
return false;
}
void init() {
scanf("%d%d",&n,&m);
int s,b;
siji(i,,n) {
scanf("%d",&s);
siji(j,,s) {
scanf("%d",&b);
g[i].push_back(b+);
}
} }
void solve() {
init();
siji(i,,n) {
memset(used,,sizeof(used));
if(find(i)) ++ans;
}
printf("%d\n",ans);
}
int main(int argc, char const *argv[])
{
#ifdef ivorysi
freopen("stall4.in","r",stdin);
freopen("stall4.out","w",stdout);
#else
freopen("f1.in","r",stdin);
#endif
solve();
return ;
}