To find the leader of node :
Check if parent[u] == u.
If yes, is the leader. Return .
If no, move up: = parent[u], and repeat.
You climb the tree until you reach a root. The root is the leader of 's group. This takes time where is tree height. Without optimizations, could be . With path compression, stays tiny.
Space complexity is for the data structures used.