Editorial for WC '17 Contest 3 S1 - Mutual Friends
Submitting an official solution before solving the problem yourself is a bannable offence.
Let be the number of different possible friendships. There's one for each unordered pair of users, meaning that
.
is small enough (only
when
) that it's viable to consider each possible subset of these friendships. There are
such subsets, and we can consider all of them using depth-first search (recursion), or by iterating over all bitmasks from
to
.
For each considered set of friendships, we can then compute its resulting grid of mutual friend counts by iterating over all pairs of users
, and counting the number of other users
such that the set of friendships includes both unordered pairs
and
. This process takes
time. If the resulting grid of mutual friend counts exactly matches the required grid
, then we've found a valid set of friendships, meaning that we can output it and stop the depth-first search. If we finish considering all
possible sets of friendships without coming across a valid one, then the answer must be
Impossible
.
The time complexity of the algorithm described above is .
Comments