-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path012_RedPainting.rb
More file actions
63 lines (53 loc) · 1.23 KB
/
Copy path012_RedPainting.rb
File metadata and controls
63 lines (53 loc) · 1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# 012 - Red Painting(★4)
# Union-Find
class UnionFind
attr_accessor :parents #, :relations
def initialize(n)
@parents = Array.new(n, &:itself)
# @relations = {}
# n.times { @relations[_1] = [_1] }
end
def root(u)
return u if u == parents[u]
parents[u] = root(parents[u])
end
def same?(u, v)
root(u) == root(v)
end
def unite(u, v)
ru, rv = root(u), root(v)
return if ru == rv
# relations[_v] |= relations[_u]
parents[ru] = rv
end
end
H, W = gets.split.map(&:to_i)
Q = gets.to_i
QUERY = Array.new(Q) { gets.split.map(&:to_i) }
@red = Array.new(H) { [false] * W }
@uf = UnionFind.new(H * W)
def unite_red(r, c)
r, c = r - 1, c - 1
@red[r][c] = true
[[1,0], [0,1], [-1,0], [0,-1]].each do |dr, dc|
nr, nc = r + dr, c + dc
next if !nr.between?(0, H - 1) || !nc.between?(0, W - 1)
next unless @red[nr][nc]
@uf.unite(r * W + c, nr * W + nc)
end
end
def same_red?(ra, ca, rb, cb)
ra, ca = ra - 1, ca - 1
rb, cb = rb - 1, cb - 1
return false unless @red[ra][ca] && @red[rb][cb]
@uf.same?(ra * W + ca, rb * W + cb)
end
ans = []
QUERY.each do |q|
if q[0] == 1
unite_red(*q[1, 2])
else
ans << (same_red?(*q[1, 4]) ? "Yes" : "No")
end
end
puts ans