Editorial for SAC '22 Code Challenge 3 P4 - Finding Friends
Submitting an official solution before solving the problem yourself is a bannable offence.
Author:
Subtask 1
Maintain an adjacency list for all the friendships.
For a type -1
if it cannot be reached).
For a type
Time Complexity:
Subtask 2
First, realize that if two friends are connected, they have a lowest common ancestor (LCA), so precompute the required values to query for the LCA between two nodes.
Then, reversing the operations (i.e., connecting friendships instead of destroying them by processing the last query first and first query last) allows us to maintain whether friends are connected by using a disjoint-set.
For a type -1
.
For a type
Time Complexity:
Note that there are many solutions to this problem (including online solutions like link-cut tree).
Comments