I have been trying this problem and ended up with RE. can’t get where the problem which causes RE lies??? Any help would be greatly appreciated. Thanks in advance.

Here my graph is numbers, of which each cell has number of id’s both have in common,and run a dfs with president as a starting node and find out how many relatives that the president has.

Can’t get why RE exists. Thanks in advance!!!

Here lies the problem.

Here is my code :

That matrix shows the graph built in O(N**3).

Correct me if I’m doing wrong.