2
public int[] findRedundantConnection(int[][] edges) {3
UnionFind uf = new UnionFind(edges.length);4
for (int[] edge : edges) {5
if (!uf.union(edge[0], edge[1])) {6
return new int[] {edge[0], edge[1]};12
private class UnionFind {17
rank = new int[n + 1];18
root = new int[n + 1];19
for (int i = 1; i <= n; i++) {29
return root[x] = find(root[x]);32
boolean union(int x, int y) {36
if (rank[rootX] > rank[rootY]) {37
root[rootY] = root[rootX];38
} else if (rank[rootY] > rank[rootX]) {39
root[rootX] = root[rootY];41
root[rootY] = root[rootX];