These are just suggestions.
I would prefer that you not read this page,
and do it your own way.
You read in the first line to determine the size of the problem, which
we call n. Your arrays must then have size n or more. In the discussion
below, I will assume that the array indices are from 1 to n, since the names
of the vertices are 1 .. n. If you use indices from 0 to n-1, you will have
to make appropriate modifications.
I used 3 arrays indexed from 1 to n:
parent[i]: the parent of vertex i in the disjoint set data structure.
If i is a leader, you can let parent[i] = i, or a sentinel value.
rank[i]: a positive integer, which is only relevant if i is a leader.
An alternative implementation uses size[i], the number of vertices
in the set whose leader is i.
next[i] = j if i and j are in the same set. These are the links of
a linked list, used only for the final output.
Steps of the algorithm:
1. Initialize parent[i] = i (or the default value) for each i.
2. Initialize rank[i] = 1 (or size[i] = 1) for each i.
3. Read the remaining lines of the input file, one at a time.
a. Read in i and j, numbers in the range 1 .. n.
b. Let u = find(i). Be sure that find uses path compression.
c. Let v = find(j).
d. If u is not equal to v, execute union(u,v).
(If you use rank):
i. If rank[u] > rank[v], set parent[v] = u.
ii. If rank[u] < rank[v], set parent[u] = v.
ii. If rank[u] = rank[v], set parent[v] = u, and
incement rank[u] by 1.
(If you use size):
i. If size[u] >= size[v], set parent[v] = u, and
let size[u] = size[u] + size[v].
i. If size[u] < size[v], set parent[u] = v, and
let size[v] = size[u] + size[v].
4. Construct a linked list, using next, for each disjoint set,
and then print them out.