Swift Union Find
Code\n\nclass Solution {\n   \n    var union: [Int] = []\n    var ranks: [Int] = []\n\n    func distanceLimitedPathsExist(_ n: Int, _ edgeList: [[Int]], _ queri
# Code\n```\nclass Solution {\n   \n    var union: [Int] = []\n    var ranks: [Int] = []\n\n    func distanceLimitedPathsExist(_ n: Int, _ edgeList: [[Int]], _ queries: [[Int]]) -> [Bool] {\n\n        union = Array(repeating: 0, count: n).enumerated().map { $0.offset }\n        ranks = Array(repeating: 1, count: n)\n        var answers = Array(repeating: false, count: queries.count)\n        var edgeList = edgeList.sorted { $0[2] < $1[2] }.map { (from: $0[0], to: $0[1], limit: $0[2]) }\n\n        var queries = queries.enumerated()\n            .map { index, query in (index: index, from: query[0], to: query[1], limit: query[2]) }\n            .sorted { $0.limit < $1.limit }\n\n        var edgeListIndex = 0\n        for i in (0..<answers.count) {\n            let query = queries[i]\n            while edgeListIndex < edgeList.count && edgeList[edgeListIndex].limit < query.limit {\n                union(edgeList[edgeListIndex].from, edgeList[edgeListIndex].to)\n                edgeListIndex += 1\n            }\n            answers[query.index] = areConnected(query.from, query.to)\n        }\n\n        return answers\n    }\n\n    func root(of node: Int) -> Int {\n        var node = node\n        while node != union[node] {\n            node = union[node]\n        }\n\n        return node\n    }\n\n    func areConnected(_ p: Int, _ q: Int)-> Bool {\n        root(of: p) == root(of: q)\n    }\n\n    func union(_ i: Int, _ j: Int) {\n        if i == j { return }\n        let jRoot = root(of: j)\n        let iRoot = root(of: i)\n        \n        if jRoot == iRoot { return }\n        if ranks[iRoot] < ranks[jRoot] {\n            union[iRoot] = jRoot\n            ranks[jRoot] += ranks[iRoot] \n        } else {\n            union[jRoot] = iRoot\n            ranks[iRoot] += ranks[jRoot]\n        }\n    }\n}\n```
['Swift']