# 并查集

Written by    22:29 October 27, 2015

### 来看一道理论题目：

4-0 6-2 8-4 9-4 3-5 9-5 5-2 1-2 7-1

（题目来自Coursera上的北大数据结构与算法课程）

0 1 2 3 4 5 6 7 8 9 //初始化，每个结点的祖宗结点都是本身

4 1 2 3 4 5 6 7 8 9//第一次make union，0和4合并，0的祖宗结点变成4的祖宗结点，即4

4 1 6 3 4 5 6 7 8 9

4 1 6 3 4 5 6 7 4 9

4 1 6 3 4 5 6 7 4 4

4 1 6 3 4 3 6 7 4 4

4 1 6 4 4 3 6 7 4 4

4 1 6 4 4 3 4 7 4 4

4 4 6 4 4 3 4 7 4 4

4 4 6 4 4 3 4 4 4 4

### 然后再来两道并查集入门题目：

POJ2524

Ubiquitous Religions
 Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 29142 Accepted: 14037

Description

There are so many different religions in the world today that it is difficult to keep track of them all. You are interested in finding out how many different religions students in your university believe in.

You know that there are n students in your university (0 < n <= 50000). It is infeasible for you to ask every student their religious beliefs. Furthermore, many students are not comfortable expressing their beliefs. One way to avoid these problems is to ask m (0 <= m <= n(n-1)/2) pairs of students and ask them whether they believe in the same religion (e.g. they may know if they both attend the same church). From this data, you may not know what each person believes in, but you can get an idea of the upper bound of how many different religions can be possibly represented on campus. You may assume that each student subscribes to at most one religion.

Input

The input consists of a number of cases. Each case starts with a line specifying the integers n and m. The next m lines each consists of two integers i and j, specifying that students i and j believe in the same religion. The students are numbered 1 to n. The end of input is specified by a line in which n = m = 0.

Output

For each test case, print on a single line the case number (starting with 1) followed by the maximum number of different religions that the students in the university believe in.

Sample Input

Sample Output

Hint

Huge input, scanf is recommended.

Source

### 代码

POJ1611

The Suspects
 Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 28635 Accepted: 13949

Description

Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, was recognized as a global threat in mid-March 2003. To minimize transmission to others, the best strategy is to separate the suspects from others.
In the Not-Spreading-Your-Sickness University (NSYSU), there are many student groups. Students in the same group intercommunicate with each other frequently, and a student may join several groups. To prevent the possible transmissions of SARS, the NSYSU collects the member lists of all student groups, and makes the following rule in their standard operation procedure (SOP).
Once a member in a group is a suspect, all members in the group are suspects.
However, they find that it is not easy to identify all the suspects when a student is recognized as a suspect. Your job is to write a program which finds all the suspects.

Input

The input file contains several cases. Each test case begins with two integers n and m in a line, where n is the number of students, and m is the number of groups. You may assume that 0 < n <= 30000 and 0 <= m <= 500. Every student is numbered by a unique integer between 0 and n−1, and initially student 0 is recognized as a suspect in all the cases. This line is followed by m member lists of the groups, one line per group. Each line begins with an integer k by itself representing the number of members in the group. Following the number of members, there are k integers representing the students in this group. All the integers in a line are separated by at least one space.
A case with n = 0 and m = 0 indicates the end of the input, and need not be processed.

Output

For each case, output the number of suspects in one line.

Sample Input

Sample Output

Source

### 题意

n个学生的编号分别为0, 1, 2, … n-1然后在m个输入里面，开始先输入一个k，然后在接下来输入k个学生的编号，表示这些学生的是属于同一个group，每个学生可以同时属于多个group，然后问有多少个学生与编号为0的学生有关系（直接或者间接地在同一个group，比如0和1在同一个group，1又和2在同一个group，则0和2有关系）

### 代码

Category : acmstudy

Tags :