Skip to content

Commit f4938fd

Browse files
committed
0684
1 parent 37c9b59 commit f4938fd

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# @param {Integer[][]} edges
2+
# @return {Integer[]}
3+
def find_redundant_connection(edges)
4+
uf = UnionFind.new(edges.size)
5+
edges.each { |u, v|
6+
return [u, v] if uf.find(u) == uf.find(v)
7+
8+
uf.union(u, v)
9+
}
10+
end
11+
12+
class UnionFind
13+
attr_reader :indexes
14+
15+
def initialize(n)
16+
@indexes = (0..n).to_a
17+
end
18+
19+
def find(x)
20+
@indexes[x] = find(@indexes[x]) if x != @indexes[x]
21+
@indexes[x]
22+
end
23+
24+
def union(x, y)
25+
@indexes[find(y)] = find(x)
26+
end
27+
end

0 commit comments

Comments
 (0)