6 lines with Unicode-Find
Accepted in 5064 ms. As long as LeetCode lets me, I\'m gonna use Unicode-Find every single time :-P\n\ndef distanceLimitedPathsExist(self, n, E, Q):\n    s = \'
Accepted in 5064 ms. As long as LeetCode lets me, I\'m gonna use Unicode-Find every single time :-P\n```\ndef distanceLimitedPathsExist(self, n, E, Q):\n    s = \'\'.join(map(chr, range(n)))\n    *map(list.append, Q, count()),\n    for u, v, d, *j in sorted(Q + E, key=itemgetter(2)):\n        if j: Q[j[0]] = s[u] == s[v]\n        else: s = s.replace(s[u], s[v])\n    return Q\n```\n(This is Python3, I just didn\'t like the look of the long cluttered original function signature.)\n\nNo need to merge the sorted edges and queries on our own, we can just put the queries first and rely on Timsort\'s stability. I annotate the queries with their index, both to distinguish them from the edges and to know where to write their results.\n\nLOL, my results are *off the charts*. Literally. On opposite ends! My *time* of 5064 ms is off the time distribution chart\'s *right* end of about 3800 ms, and my *memory usage* of 53.1 MB is off the memory distribution\'s *left* end of 60.0 MB. Well, a string is a very compact data structure.\n![image](https://assets.leetcode.com/users/images/2c1ca99d-8aa7-4210-a9b4-b5fd2774ba84_1608616494.9549015.png)\n
['Python']