Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.
Authors: jimgao, d
Knowledge required: complete search or disjoint set
When one has Ebola, everyone in his class can potentially get Ebola. With that observation, we can express each class as a set. When person
and
are in the same class, we can simply merge the 2 sets that
and
belongs to. Hence, the intended solution to this problem is to use disjoint set to manage the contents of the sets.
The disjoint set data structure has 2 operations
and
. When person
and
are in the same class, we can simply do
. After processing all the classes, we can simply compare the value of
and
to determine if person
is infected.
A properly implemented disjoint set has a time complexity of
for each operation, where
is the Inverse Ackermann function, which is smaller than
for all reasonable values of
.
Time Complexity: 
Alternate solution
Create a vertex for each student and each class. If a student is in a class, then create an edge between that student and that class. In total, there are
vertices and
edges.
Next, do a graph search starting at student
. Make sure to output the visited students. The visited classes can be ignored.
Time Complexity: 
Comments