Disjoint sets (union-find) · Compiler

Disjoint sets (union-find)

Start with n singleton sets labeled 1 through n. For each pair, unite the sets containing its two labels. A join within one set leaves it unchanged. Find the size of the set containing t after all joins.

Input
The first line contains n, m, and t (1 <= n <= 100, 0 <= m <= 200, 1 <= t <= n). Each of the next m lines contains two labels in the range 1 through n. Pairs may repeat or contain the same label twice.
Output
Print the number of labels in the final set containing t.

Compiler Explorer runs your code when you run or check it.

Loading your exercise draft…