Python | Easy to Understand | Fastest | Optimal Solution
Python | Easy to Understand | Fastest | Optimal Solution\n\nclass UnionFindSet:\n    def __init__(self, n=0):\n        self.parents = {}\n        self.ranks = {
# Python | Easy to Understand | Fastest | Optimal Solution\n```\nclass UnionFindSet:\n    def __init__(self, n=0):\n        self.parents = {}\n        self.ranks = {}\n        self.count = 0\n        for i in range(n):\n            self.add(i)\n\n    def add(self, p):\n        self.parents[p] = p\n        self.ranks[p] = 1\n        self.count += 1\n\n    def find(self, u):\n        if u != self.parents[u]:\n            self.parents[u] = self.find(self.parents[u])\n        return self.parents[u]\n\n    def union(self, u, v):\n        pu, pv = self.find(u), self.find(v)\n        if pu == pv: \n            return False\n        if self.ranks[pu] < self.ranks[pv]:\n            self.parents[pu] = pv\n        elif self.ranks[pu] > self.ranks[pv]:\n            self.parents[pv] = pu\n        else:        \n            self.parents[pv] = pu\n            self.ranks[pu] += 1\n        self.count -= 1\n        return True\n    \n# UnionFind + Kruskal + Enumerate edges\n# O(ElogE + E^2 + E^2)\nclass Solution:\n    def findCriticalAndPseudoCriticalEdges(self, n: int, edges: List[List[int]]) -> List[List[int]]:\n        # sort edges in asc order based on weight\n        edges = [(u, v, w, i) for i, (u, v, w) in enumerate(edges)]\n        edges.sort(key=lambda x: x[2])\n        \n        # do not use this edge\n        def find_mst_without_this_edge(edge_idx):\n            union_find_set = UnionFindSet(n)\n            ans = 0\n            for i, (u, v, w, _) in enumerate(edges):\n                # do not use this edge\n                if i == edge_idx:\n                    continue\n                if union_find_set.union(u, v):\n                    ans += w\n            parent = union_find_set.find(0)\n            return ans if all(union_find_set.find(i) == parent for i in range(n)) else inf\n        \n        # need to use this edge\n        def find_mst_with_this_edge(edge_idx):\n            union_find_set = UnionFindSet(n)\n            # use this edge first\n            u0, v0, w0, _ = edges[edge_idx]\n            ans = w0\n            union_find_set.union(u0, v0)\n            for i, (u, v, w, _) in enumerate(edges):\n                # do not use this edge\n                if i == edge_idx:\n                    continue\n                if union_find_set.union(u, v):\n                    ans += w\n            parent = union_find_set.find(0)\n            return ans if all(union_find_set.find(i) == parent for i in range(n)) else inf\n        \n        # normal MST total weight\n        base = find_mst_without_this_edge(-1)\n        cri, p_cri = set(), set()\n        for i in range(len(edges)):\n            wgt_excl = find_mst_without_this_edge(i)\n            # if not included, MST total weight would increase\n            if wgt_excl > base:\n                cri.add(edges[i][3])\n            else:\n                wgt_incl = find_mst_with_this_edge(i)\n                # with this edge, MST total weight doesn\'t change\n                if wgt_incl == base:\n                    p_cri.add(edges[i][3])\n    \n        return [cri, p_cri]\n```
['Python', 'Python3']