home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Usenet 1994 October
/
usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso
/
unix
/
volume17
/
contest-prog
/
part02
/
prob16.txt
< prev
next >
Wrap
Text File
|
1989-02-06
|
1KB
|
22 lines
Problem 16: Equivalence classes.
Suppose that we have a set of N objects {a[i]}. We are also given M statements
of the form a[i] == a[j] . Let us assume that the objects have been mapped into
the integers 1 .. N by some manner. For example, with N = 19 and M = 16, we
might have the following objects and relationships:
18 = 12 16 = 14 8 = 18 16 = 6
6 = 10 9 = 1 17 = 4 16 = 17
8 = 2 3 = 13 9 = 11 3 = 8
11 = 5 7 = 19 3 = 9 19 = 15
Write a program that reads M and N, followed by the M pairings i = j,
with i and j both in the range 1 .. N. Your program should compute which
objects fall into which classes of equivalent objects, and should then
print out these classes, as follows: objects sorted within each class,
and classes printed in order of their first members. Thus, for the example
above, the output would be:
1 2 3 5 8 9 11 12 13 18
4 6 10 14 16 17
7 15 19