Created
October 14, 2017 14:03
-
-
Save whatalnk/61c9ba0c29b5a6be6b0f7b3dfe248eb5 to your computer and use it in GitHub Desktop.
AtCoder ABC #075
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
a, b, c = gets.chomp.split(" ").map(&:to_i) | |
if a == b | |
puts c | |
elsif b == c | |
puts a | |
else | |
puts b | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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