Editorial for Baltic OI '03 P4 - The Gangs
Submitting an official solution before solving the problem yourself is a bannable offence.
This is a logic puzzle about equalities (friends) and inequalities (enemies).
The story tells us that the relation "friend of" is symmetric: two gangsters are friends (of
each other). Then "friend of" can also be assumed reflexive: a gangster who is not a friend of
himself cannot be a member of any gang, and this leads to the minimum answer which
we must try to improve. Ethics code
means that "friend of" is transitive. Hence "friend of" is
an equality relation. It follows that a gang is an equivalence class of this relation.
These classes can be maintained as the well-known Merge-Find data structure. Initially each gangster is alone in his own class. Each fact F p q
merges the classes for and
.
Such merge operations include not only those given explicitly as input but also those that
are given implicitly by ethics code and the enemy facts
E p q
given as input: if and
are
enemies, and
and
are also enemies, then
and
are friends. Again, the story tells us that "enemy of" is symmetric.
These merge operations are exactly those that must be performed to represent the input.
Hence the output is the number of still distinct classes in the resulting data structure.
However, we must also satisfy each fact
E p q
: if and
end up in the same class, then the
input is inconsistent, since
and
should be both friends and enemies, and the story tells
us that these two relations are mutually exclusive. Hence the correct output is the minimum
answer
in this case.
A rough complexity analysis is as follows. The algorithm needs space: the Merge-Find
structure for the gangster, and a designated enemy for every gangster. This designated enemy
is used for maintaining the enemy information efficiently: by ethics code
, all my enemies are friends of each other, so it suffices to remember the first of my enemies, and merge all later enemies with him. Initially nobody has a designated enemy.
The following algorithm needs time, where
is the inverse of Ackermann's function.
First, it takes steps to initialize the data structures. Then it processes each of the
facts, one by one, as follows. A friend fact
F p q
is processed by merging the classes of and
.
An enemy fact
E p q
is in turn processed as follows. Consider first. If
has no designated enemy
yet, then make
the designated enemy of
. Otherwise merge the classes of
and
.
The other gangster
is processed symmetrically.
This generates less than merge operations into a Merge-Find structure of
elements, and this takes
total time.
Then we can check that each enemy fact E p q
is satisfied by checking that no gangster is in the same class as his designated enemy (if any). If such a gangster does exist, then .
Otherwise the correct answer is the number of different classes. It can be computed by
counting the number of those gangster nodes in the Merge-Find structure that are the roots of
the trees representing the classes.
In either case,
can be computed in
steps.
Hence this problem can be solved with linear memory with respect to and almost linear time with respect to
, so their upper bounds could be large in order to separate the optimal solutions from the slower ones.
Comments