"Codevs" 2776 looking for representative yuan

Source: Internet
Author: User

Title Description Description

Guangzhou second middle Su Yuan Experimental school has a total of n societies, numbered 1 to n respectively.
Guangzhou second middle Su Yuan Experimental school has a total of M individuals, which are numbered 1 to M respectively. Each person may participate in one or more societies, or may not participate in any community.
Each community needs to choose a representative. Brother Qian wants more people to be represented.

Enter a description Input Description

The first line enters two numbers n and M.
The following n rows have a number of rows per line, and these numbers are positive integers that do not exceed M. Where the number of line I represents all members of the community I. Each line ends with a 0.

Output description Output Description

The maximum number of people that can be represented is the output.

Sample input Sample Input

4 4
1 2 0
1 2 0
1 2 0
1 2 3) 4 0

Sample output Sample Output

3

Data range and Tips Data Size & Hint

Each test point 1s

Data range
n,m<=200

(。?_?。 ) Naked Hungarian algorithm, the main idea is recursion;

Post the CSDN on the previous Hungarian algorithm, it's easy to understand

A series of interesting writing algorithms--Hungarian algorithm

Put the code (σ ' д′) σ

1#include <cstdio>2#include <cstring>3#include <algorithm>4 using namespacestd;5 intn,m;6 BOOLjoin[201][201];//Join[i][j] Record J student whether join I community;7 BOOLv[201];//whether the student has been judged;8 BOOLr[201];//whether the society can find a representative that is not duplicated;9 intstu[201];//record the student representative's community number;Ten BOOLFindintx) One { A      for(intI=1; i<=m;i++) -     { -         if(Join[x][i]&&!v[i])//If a student joins the club and the student has not judged it (there is no need to judge it if it has been judged) the         { -v[i]=1; -             if(stu[i]==0|| Find (Stu[i]))//the student's previously represented community can also find other students as representatives; -             { +stu[i]=x; -                 return true; +             } A         } at     } -     return false; - } - intMain () - { -scanf"%d%d",&n,&m); inmemset (Join,0,sizeof(join)); -      for(intI=1; i<=n;i++) to     { +         intj=1; -          while(j!=0) the         { *scanf"%d",&j); $join[i][j]=1;Panax Notoginseng          } -     } theMemset (Stu,0,sizeof(Stu)); +     intans=0; A      for(intI=1; i<=n;i++) the     { +memset (V,0,sizeof(v)); -r[i]=find (i); $         if(R[i])//record the number of associations that can be found, that is, the number of delegates not duplicated; $         { -ans++; -         } the     } -printf"%d", ans);Wuyi     return 0; the}

"Codevs" 2776 looking for representative yuan

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.