Acdream 1685 Multi-ethnic kingdoms (figure, DFS, and set variants)

Source: Internet
Author: User

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 a group of people, each person will be C language (c>=0), the problem is, to allow them to communicate with each other (can be translated by others, only need to speak to those who can understand your words, let him convey for you). Q, if you consume 1 points of energy to allow someone to learn a language, how much energy is consumed to communicate?

Idea: The data is also relatively small, directly on the BFS, maintenance of two tables, one is indexed by the language number of the table, indicating the language of the person. A table that is indexed by a person's number indicates the language of the person. From the first person began to pull out all the connected people, from the person indexed to his language, in these languages to index all the people who speak the language, all dragged out, to set these people to browse. If there is one left, there must be no contact. In fact, it is to ask how many connected components, but this problem does not give a direct picture, but to give them the language, inconvenient to use and check set, if time-consuming to search all over to establish the adjacency table, why not as a direct search to arrive at the answer.

1#include <bits/stdc++.h>2 #defineLL Long Long3 using namespacestd;4 Const intn=102;5vector< vector<int> >Lang;6vector< vector<int> >Peop;7  8 BOOLVis[n];9 intt, N, M, Num, C;Ten   One voidDFS (inte) A { -      for(intI=0; I<peop[e].size (); i++)//The language of the meeting -     { the         intR=Peop[e][i]; -          for(intj=0; J<lang[r].size (); J + +)//All The people who speak the language are friends. -         { -             intW=LANG[R][J];//people who can speak R. +             if( !Vis[w]) -             { +vis[w]=true; A DFS (w); at             } -         } -     } - } -   - intcal () in { -     intcnt=-1; tomemset (Vis,0,sizeof(Vis)); +      for(intI=1; i<=n; i++)//everyone -     { the         if(Peop[i].empty ())//These people are not talking. *         { $vis[i]=true;Panax Notoginseng             Continue; -         } the         if(!Vis[i]) +         { Avis[i]=true; thecnt++; +DFS (i);//violence against it 9 families -         } $     } $     returnMax (CNT,0);//It's not a negative!  - } -   the intMain () - {Wuyi     //freopen ("E://input.txt "," R ", stdin); thevector<int>tmp; -Cin>>T; Wu      while(t--) -     { About         intCnt=0;//These people must learn a language $ peop.clear (); Lang.clear (); -scanf"%d%d",&n,&m); -          for(intI=0; I<= (N&GT;M?N:M); i++) Peop.push_back (TMP), Lang.push_back (TMP);//Initialize -          for(intI=1; i<=n; i++)//everyone A         { +scanf"%d",&num); the             if(!num) cnt++;//Note that the pit is here: if there are two people and no language, then they must learn one, that is, 2 points of energy, not 1.  -              for(intj=0; j<num; J + +)//language of the meeting $             { thescanf"%d",&c); theLang[c].push_back (i);//people who can speak C. thePeop[i].push_back (c);//language of the first person the             } -         } inCout<<cal () +cnt<<Endl; the     } the     return 0; About}
AC Code

Acdream 1685 Multi-ethnic kingdoms (figure, DFS, and set variants)

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.