Different Approach than normal graph traversal


#1

I have used the data structure Disjoint Set Union (Union Find).
https://www.hackerearth.com/practice/notes/disjoint-set-union-union-find/

The complexity can be reduced to Log*N which is faster than LogN.


#2

Can you elaborate on how you did it with disjoint set union ? Is it that you consider each node having a value of ‘X’ as a parent individually first and then merge the adjacent nodes which have ‘X’ on them and finally count how many total connected components are there ? Can you share the code ?