Hungarian algorithm cogs 886. [Usaco 4.2] the perfect barn

Source: Internet
Author: User
Tags stdin

886. [Usaco 4.2] Perfect barn ★★☆ input file: stall4.inOutput file: stall4.outSimple Comparison
Time limit: 1 s memory limit: MBUSACO/STALL4 (translated by Felicia Crazy)

Describe

Farmer John had just built his new barn last week, and he used the latest milking techniques. Unfortunately, every barn is different due to engineering problems. The first week, farmer John casually let the cows into the barn, but the problem quickly revealed: Every cow is only willing to milk in the barn they like. Last week, farmer John had just gathered information about the cows ' hobbies (which cows would like to produce milk). A barn can only hold a cow, of course, a cow can only produce milk in a barn.

Give information about the dairy cows ' hobbies and calculate the maximum allocation scheme.

Format

Program NAME:STALL4

INPUT FORMAT:

(File stall4.in)

First line two integers, n (0 <= n <= 200) and M (0 <= m <= 200). N is the farmer John's cow number, M is the number of cattle in the new barn.
Second line to line n+1

n rows, one cow per line. The first number (Si) is the number of cattle cattle that the cow is willing to produce milk in (0 <= si<= M). The number of SI in the back indicates the numbers of these cattle pens. The number of the barn is limited to the interval (1..M), and in the same row, a barn is not listed two times.

OUTPUT FORMAT:

(File stall4.out)

Only one row. Outputs an integer that represents the maximum number of cattle pens that can be allocated.

SAMPLE INPUT (file stall4.in)

5 5

2 2 5

3 2 3 4

2 1 5

3 1 2 5

1 2

SAMPLE OUTPUT (file stall4.out) 4

1 #defineN 2052#include <iostream>3 using namespacestd;4#include <cstdio>5#include <cstring>6#include <vector>7 intN,m,lin[n];8vector<int>Edge[n];9 BOOLVis[n];Ten intRead () One { A     intret=0, ff=1; -     Chars=GetChar (); -      while(s<'0'|| S>'9') the     { -         if(s=='-') ff=-1; -s=GetChar (); -     } +      while(s>='0'&&s<='9') -     { +ret=ret*Ten+s-'0'; As=GetChar (); at     } -     returnret; - } - voidInpu () - { -N=read (); m=read (); in     ints,x; -      for(intI=1; i<=n;++i) to     { +s=read (); -          for(intj=1; j<=s;++j) the Edge[i].push_back (read ()); *     } $ }Panax Notoginseng BOOLXylfind (intk) - { the     intsiz=edge[k].size (); +      for(intI=0; i<siz;++i) A     { the         intv=Edge[k][i]; +         if(!Vis[v]) -         { $vis[v]=true; $             if(!lin[v]| |Xylfind (Lin[v])) -             { -lin[v]=K; the                 return true; -             }Wuyi         } the     } -     return false; Wu } - intMain () About { $Freopen ("stall4.in","R", stdin); -Freopen ("Stall4.out","W", stdout); - Inpu (); -     intans=0; A      for(intI=1; i<=n;++i) +     { thememset (Vis,0,sizeof(Vis)); -         if(Xylfind (i)) ans++; $     } theprintf"%d\n", ans); the fclose (stdin); the fclose (stdout); the     return 0; -}

Hungarian algorithm cogs 886. [Usaco 4.2] the perfect barn

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.