So, I was trying to solve the MAGICSTR problem here.
After reading the editorial and going through some blogs, I made an implementation of the Union-Find Data Structure in Python and tried to solve the problem using it. But I am getting a TLE. Here is the link to my last solution. In the data structure, I used both path compression and union by rank. Please help.