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 of the stalls in the new barn is different. For the first week, Farmer John randomly assigned cows to stalls, but it quickly became clear this any given cow is only Willing to produce milk in certain stalls. For the last week, Farmer John had been collecting data on which cows is willing to produce milk in which stalls. A stall may is assigned to one cow, and, of course, a cow is only assigned to one stall.
Given the preferences of the cows, compute the maximum number of milk-producing assignments of cows to stalls that's poss Ible.
Program Name:stall4input FORMAT
Line 1: |
One line with the integers, n (0 <= n <=) and M (0 <= M <= 200). N is the number of cows, Farmer John has and M are 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, the cow is willing to produce milk in (0 <= Si <= M) . The subsequent Si integers on this line was the stalls in which that cow was willing to produce milk. The stall numbers is integers in the range (1..M), and no stall would be listed twice for a given cow. |
SAMPLE INPUT (file stall4.in)
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
——————————————————————————————————
Binary map matching Hungarian algorithm template
Algorithm Description:
For the current point x, select a point y to match, and if this point y matches another point x ', recursively know for X ' to find another assignment or cannot find another assignment
1 /*2 Id:ivorysi3 PROG:STALL44 lang:c++5 */6#include <iostream>7#include <cstdio>8#include <cstring>9#include <queue>Ten#include <Set> One#include <vector> A#include <cmath> - #defineINF 0x7fffffff - #defineIvorysi the #defineSiji (i,x,y) for (int i= (x); i<= (y); ++i) - #defineGongzi (j,x,y) for (int j= (x); j>= (y);--j) - #defineXiaosiji (i,x,y) for (int i= (x);i< (y); ++i) - #defineSigongzi (j,x,y) for (int j= (x);j> (y);--j) + #defineP (x) (x) * (x) - using namespacestd; +vector<int> g[505]; A intused[505], from[505]; at intN,m,ans; - BOOLFindintx) { -used[x]=1; - for(intI=0; I<g[x].size (); + +i) { - if( from[G[x][i]]) { - if(used[ from[G[x][i]] ==0&& Find ( from[G[x][i]])) { in from[g[x][i]]=x; - return true; to } + } - Else { the from[g[x][i]]=x; * return true; $ }Panax Notoginseng } - return false; the } + voidinit () { Ascanf"%d%d",&n,&m); the ints,b; +Siji (I,1, N) { -scanf"%d",&s); $Siji (J,1, s) { $scanf"%d",&b); -G[i].push_back (b + $); - } the } - Wuyi } the voidsolve () { - init (); WuSiji (I,1, N) { -memset (Used,0,sizeof(used)); About if(Find (i)) + +ans; $ } -printf"%d\n", ans); - } - intMainintargcChar Const*argv[]) A { + #ifdef Ivorysi theFreopen ("stall4.in","R", stdin); -Freopen ("Stall4.out","W", stdout); $ #else theFreopen ("f1.in","R", stdin); the #endif the solve (); the return 0; -}
Usaco 4.2 The Perfect Stall (binary map matching Hungarian algorithm)