2
static class UnionFind {11
for (int i = 0; i < n; i++) {21
parent[x] = find(parent[x]);26
boolean union(int X, int Y) {33
if (rank[x] < rank[y]) {35
} else if (rank[y] < rank[x]) {45
boolean isConnected() {50
static class Edge implements Comparable<Edge> {55
Edge(int u, int v, int wt) {61
public int compareTo(Edge o) {62
return this.wt - o.wt;66
public int buildMST(int n, int[][] edges, int[] edgeSkip, int[] edgePick) {67
PriorityQueue<Edge> pq = new PriorityQueue<>();69
for (int[] edge : edges) {70
if (edge == edgeSkip) {72
} else if (edge == edgePick) {78
pq.add(new Edge(u, v, wt));81
UnionFind uf = new UnionFind(n);84
if (edgePick != null) {85
uf.union(edgePick[0], edgePick[1]);88
while (pq.size() > 0) {89
Edge rem = pq.remove();90
if (uf.union(rem.u, rem.v) == true) {95
if (uf.isConnected() == true) {98
return Integer.MAX_VALUE;102
public List<List<Integer>> findCriticalAndPseudoCriticalEdges(int n, int[][] edges) {103
int mstCost = buildMST(n, edges, null, null);105
ArrayList<Integer> critical = new ArrayList<>();106
ArrayList<Integer> pcritical = new ArrayList<>();108
for (int i = 0; i < edges.length; i++) {109
int[] edge = edges[i];111
int mstCostWithoutEdge = buildMST(n, edges, edge, null);112
if (mstCostWithoutEdge > mstCost) {113
critical.add(i); // Critical edge index115
int mstCostWithEdge = buildMST(n, edges, null, edge);116
if (mstCostWithEdge > mstCost) {119
pcritical.add(i); // pseduo critical edge index124
List<List<Integer>> res = new ArrayList<>();