Skip to content

Instantly share code, notes, and snippets.

@whatalnk
Created October 14, 2017 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save whatalnk/61c9ba0c29b5a6be6b0f7b3dfe248eb5 to your computer and use it in GitHub Desktop.
Save whatalnk/61c9ba0c29b5a6be6b0f7b3dfe248eb5 to your computer and use it in GitHub Desktop.
AtCoder ABC #075
a, b, c = gets.chomp.split(" ").map(&:to_i)
if a == b
puts c
elsif b == c
puts a
else
puts b
end
H, W = gets.chomp.split(" ").map(&:to_i)
@m = []
@ans = Array.new(H){Array.new(W, 0)}
bomb = []
H.times do |i|
row = gets.chomp.split("")
W.times do |j|
if row[j] == "#"
@ans[i][j] = row[j]
bomb << [i, j]
end
end
@m << row
end
d = [[0, -1], [1, -1], [1, 0], [1, 1], [0, 1], [-1, 1], [-1, 0], [-1, -1]]
bomb.each do |i, j|
d.each do |dx, dy|
nx = j + dx
ny = i + dy
if nx >= 0 && nx < W && ny >= 0 && ny < H && @m[ny][nx] == "."
@ans[ny][nx] += 1
end
end
end
@ans.each do |row|
puts row.join("")
end
N, M = gets.chomp.split(" ").map(&:to_i)
node = Struct.new("Node", :to)
edges = []
M.times do
edges << gets.chomp.split(" ").map(&:to_i)
end
# Union Find
def find(x)
if @par[x] == x
return x
else
@par[x] = find(@par[x])
return @par[x]
end
end
def unite(x, y)
x = find(x)
y = find(y)
if x == y
return
end
if @rank[x] < @rank[y]
@par[x] = y
else
@par[y] = x
if @rank[x] == @rank[y]
@rank[x] += 1
end
end
end
def same(x, y)
return find(x) == find(y)
end
ans = 0
M.times do |i|
@g = Array.new(N+1){node.new([])}
@par = (0..N).to_a
@rank = Array.new(N+1, 0)
M.times do |j|
next if i == j
a, b = edges[j]
@g[a].to << b
@g[b].to << a
end
(1..N).each do |n1|
@g[n1].to.each do |n2|
unite(n1, n2)
end
end
nroot = 0
(1..N).each do |root|
if root == @par[root]
nroot += 1
end
end
if nroot != 1
ans += 1
end
end
puts ans
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment