Java Simple Union-FInd
Just use union-find to check if that is critical connect;\n1, if by find the root partent for the 2 nodes that one edge connected, it is not critical nonnect, c
Just use union-find to check if that is critical connect;\n1, if by find the root partent for the 2 nodes that one edge connected, it is not critical nonnect, could be removed, res++;\nelse union it, islands--;\n2, union and count for type 3 first.\n\n\n```\nclass Solution {\n    int res = 0, na, nb, psa[], psb[], es[][];  // na, islands of alice, nb, islands of bob, etc...\n    public int maxNumEdgesToRemove(int n, int[][] edges) {\n        na = nb = n; psa = new int[n + 1]; psb = new int[n + 1]; es = edges;\n        for (int i = 1; i <= n; i++) {\n            psa[i] = i;   // init, single islands for each node;\n            psb[i] = i;\n        }\n        count(3);  // union and count for type 3 first.\n        count(2);\n        count(1);\n        if (na != 1 || nb != 1) return -1; // either Alice or bob could not traverse\n        return res;\n    }\n    \n    private void count(int type) {\n        for (int[] e : es) \n            if (e[0] == type) {\n                if (type == 3 && !union(1, e) && !union(2, e)) res++;\n                else if (!union(type, e)) res++;\n            }\n    }\n    \n    private boolean union(int type, int[] e) {\n        int ps[] = type == 1 ? psa : psb, up = find(e[1], ps), vp = find(e[2], ps);\n        if (up == vp) return false;  // don\'t need to union, non-critical edges, possible remove;\n        ps[up] = vp;\n        if (type == 1) na--;\n        else nb--;\n        return true;\n    }\n    \n    private int find(int node, int[] ps) {\n        int p = ps[node];\n        if (node != p)  ps[node] = find(ps[node], ps);\n        return ps[node];\n    }\n}\n```
[]