F-Nana in Wonderland Series--multi-ethnic kingdom

Source: Internet
Author: User

F-Nana in Wonderland Series--multi-ethnic KingdomTime Limit:2000/1000ms (java/others) Memory Limit: 128000/64000kb (java/others) Submit Statusproblem Description

Nana finally recalled that she was Nana and not what Alice, also recalled to continue to explore the world's goals, then sneak out of the palace. Nana found that there are many nationalities in this kingdom, each nation has its own dialect, and what is more, the difference between these aspects is very far, which leads to the exchange of the people of this kingdom is very difficult. Nana carefully observed and recorded for a long time, found a total of M different languages.

Suddenly Nana found a group of geniuses in front of him discussing the problem, but the language problem caused the group to communicate very hard. Fortunately, however, these geniuses have a special ability to fully comprehend a new language as long as one unit of energy is consumed (mother no longer has to worry about my level 46 TOEFL IELTS gre! )。 So Nana's long-lost curiosity began to bubble, Nana hope you tell her, if you know the language of each person will be able to let this group of genius 22 direct or indirect communication? The so-called indirect communication refers to a number of people's translation of two people to get the message of mutual expression. If not, at least how much energy is needed to achieve it?

Input

Multiple sets of data, first a positive integer t (t<=20)

For each set of data, the first is two integers n,m (2<=n<=100,1<=m<=100), representing the number of people and the number of languages, and the number of languages from 1~m.

Next is n rows, each of which describes the person.

The first is an integer k, which indicates that the person has a K-gate language, followed by a K-integer, which is the language number that this person mastered. (0<=k<=m)

Output for each set of data, outputs an integer that allows the group to communicate with each other directly or indirectly the minimum amount of energy needed. Sample Input
22 21 205 51 22 2 32 3 42 4 51 5
Sample Output
10
Hint

Example 1 where the first person in the second language, and the second person does not have any language, so only need a second person also learn the second language can communicate, so the number of energy is 1

There are 5 people in sample 2, and these 5 people can communicate with each other directly or indirectly: 1-2-3-4-5, which is exactly a chain. So there is no need to continue learning the new language, the number of energies is 0.

Test instructions

Enter N and M for the number of individuals and languages, and then enter the language that each person has mastered, and the topic of the collection, because of the small amount of data, all using a two-dimensional array to maintain the relationship,

A_B[I][J] Indicates whether the person of number I is master of language J, 1 means mastery, and 2 means no mastery. Through the relationship, construct and check the set to determine how many edges can be formed to determine the number of sides to determine the answer,

1#include <iostream>2#include <stdio.h>3#include <stdlib.h>4#include <string.h>5 using namespacestd;6 intuset[3333],num[3333],parper[3333];7 inta_b[ the][ the];8 intFind (intx)9 {Ten     intp=x,t; One      while(uset[p]!=p) Ap=Uset[p]; -      while(x!=p) -{t=uset[x];uset[x]=p;x=t;} the     returnx; - } - intMain () - { +     intT,n,m,k,i,j,l,Get, A,b,begin; -     intnum[ the],max; +scanf"%d",&T); A      while(t--) at     { -memset (A_b,0,sizeof(A_b)); -scanf"%d%d",&n,&m); -         { -              for(i=0; i<n;i++) uset[i]=i; -              for(i=0; i<m;i++) num[i]=0; in              for(i=0; i<n;i++) -             { toscanf"%d",&k); +parper[i]=i;uset[i]=i;num[i]=1; -                  for(j=0; j<k;j++) the                 { *scanf"%d",&Get); $a_b[i][Get-1]=1;/*Record Relationships*/Panax Notoginseng                 } -             } the              for(j=0, max=0, begin=0; j<m;j++) +             {        A                  for(i=0; i<n;i++) the                 { +                     if(a_b[i][j]==1)/*find the language that the first person has mastered*/ -                     { $num[j]++; $                        if(Max<num[j]) max=Num[j]; -                         for(l=i+1; l<n;l++) -                         { the                             if(a_b[l][j]==1)/*find the same language that other people have mastered*/ -                             {WuyiA=Find (i); theb=Find (l); -                                 if(a!=b)/*determine if a new edge is generated*/ Wu                                 { -begin++;/*New Edge, the number of edges plus 1*/ AboutUset[a]=b;/*determine if a new edge is generated*/ $                                 } -                             } -                         } -                          Break; A                     } +                 } the             } -             if(begin==0) printf ("%d\n", N-Max); $             Else if(begin>=n-1) printf ("0\n"); the             Elseprintf"%d\n", N-1-Begin); the         } the     } the     return 0; -}
View Code

F-Nana in Wonderland Series--multi-ethnic kingdom

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.