Concise code to find criticality
IntuitionClean code to solve the problem using disjoint set data structure.ApproachCalculate total weight of MST before and after removing an edge to understand
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
Clean code to solve the problem using disjoint set data structure.
# Approach
<!-- Describe your approach to solving the problem. -->
Calculate total weight of MST before and after removing an edge to understand its criticality.
# Complexity
- Time complexity: $$O(m^2)$$
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity: $$O(m)$$
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```python3 []
class Solution:
    def findCriticalAndPseudoCriticalEdges(self, n: int, edges: List[List[int]]) -> List[List[int]]:
        parent = [i for i in range(n)]
        size = [1] * n

        def clean_env():
            nonlocal parent, size
            parent = [i for i in range(n)]
            size = [1] * n

        def find(i):
            if parent[i] != i:
                parent[i] = find(parent[i])
            return parent[i]
        
        def union(i, j):
            root1, root2 = find(i), find(j)
            if root1 == root2:
                return False

            if size[root1] < size[root2]:
                size[root2] += size[root1]
                parent[root1] = root2
            else:
                parent[root2] = root1
                size[root1] += size[root2]
            return True

        def mst_weight(prevent_edge):
            weight = 0
            count = 0
            for i in range(len(edges)):
                if i != prevent_edge:
                    if union(edges[i][0], edges[i][1]):
                        weight += edges[i][2]
                        count += 1
            return weight, count

        idx = sorted(range(len(edges)), key=lambda x: edges[x][2])
        edges = [edges[i] for i in idx]

        mst_w, _ = mst_weight(edges)
        critical = set()
        p_critical = set()

        for i in range(len(edges)):
            clean_env()
            new_w, count = mst_weight(i)
            if new_w > mst_w or count != n - 1:
                critical.add(idx[i])
            else:
                clean_env()
                union(edges[i][0], edges[i][1])
                new_w, count = mst_weight(i)
                if new_w + edges[i][2] == mst_w and count == n - 2:
                    p_critical.add(idx[i])

        return [list(critical), list(p_critical)]
        


```
['Python3']