Count reachable nodes from every node .
visited = [ 0 ] * ( n + 1 ) NEW_LINE ans = 1 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( not visited [ i ] ) : NEW_LINE INDENT ans = lcm ( ans , dfs ( i , adj , visited ) ) NEW_LINE DEDENT DEDENT return ans NEW_LINE