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

Source: Internet
Author: User

F - Nana in Wonderland Series--multi-ethnic Kingdom Time Limit: 2000/1000ms (java/others) Memory Limit: 128000/64000kb (java/others) Problem 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: There are n individuals, who have the same language, even one side, asking how many edges need to be added to make the diagram fully connected.

Solution: You can use and check the set to maintain the connectivity of the graph, or directly DFS or BFS to calculate the number of connected blocks. If there is only one connected block answer of 0 (because everyone can have no obstacle indirect or direct communication), each more connected block will add an edge, so the answer is the number of connected blocks-1, but there is a special case where all people do not speak language, then this situation requires a special sentence, the answer is the number of people learning the same language.

Note: Rank actually does not understand how to use the specific ...

1#include <stdio.h>2#include <string.h>3 #defineMAXSIZE 2054  5 intUset[maxsize],rank[maxsize];6  7 voidMakeSet (intsize)//Create a collection for each number, point to yourself, rank save rank8 {9     inti;Ten      for(i =0; I < size;i++) One     { AUset[i] =i; -Rank[i] =0; -     } the } -   - intFindintx) {//path compression, which points all elements to the root -   +     if(X! =Uset[x]) -USET[X] =find (Uset[x]); +     returnUset[x]; A   at } -   - intUnionset (intXinty) {//union two sets, returns whether two different sets -   -     if((x = Find (x)) = = (Y =find (Y))) -         return 0; in     if(Rank[x] >Rank[y]) -Uset[y] =x; to     Else +     { -USET[X] =y; the         if(Rank[x] = =Rank[y]) *rank[y]++; $     }Panax Notoginseng     return 1; - } the   + intMain () A { the     intt,i,j,s,k,mark,ski,n,m,p,q,l,sum; +      while(SCANF ("%d", &t) = =1) -     { $          while(t--) $         { -scanf"%d%d",&n,&m); -s=sum=0; the MakeSet (MAXSIZE); -              for(i=1; i<=n;i++)Wuyi             { thescanf"%d",&ski); -                 if(ski==0) Wusum++; -                  for(j=0; j<ski;j++) About                 { $scanf"%d",&k); -P=unionset (k + -, i); Put the language type +100 in the character I -                     if(p==0)//Description of the language other people will -                     { AQ=find (k + -);  + Unionset (I,Q); Put the character in a collection with another person who can communicate theQ=q<i?q:i; -mark=1; $                     } the                 } the             } the             if(sum==N)//The language of all people will be 0 theprintf"%d\n", n); -             Else in             { the                  for(i=1; i<=n;i++) the                 { About                     if(uset[i]==i)//the root points to itself, so satisfying is a set, otherwise it belongs to another set, that is related.  thes++; the                 } theprintf"%d\n", S-1); n a set, as long as n-1 a side. +             } -         } the     }Bayi     return 0; the}

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.