DSU11 Editorial

Check if two elements are connected

Problem Explanation

In this problem we are given N number of nodes and M number of connections between these nodes. We have to answer Q queries asking if two nodes are connected.

Logic

If we insert all the nodes with their connections into a DSU. Then, the elements in the same connected component will have the same leader. So, we can use the find function to find the leader of the two elements in the query and if they have the same leader, they will belong to the same connected component.