Skip to content

Instantly share code, notes, and snippets.

@whatalnk
Last active August 5, 2016 07:52
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/f3e2c1b0b1667590c64c72882099f21b to your computer and use it in GitHub Desktop.
Save whatalnk/f3e2c1b0b1667590c64c72882099f21b to your computer and use it in GitHub Desktop.
天下一プログラマーコンテスト2016 [AtCoder] [Ruby]
res = 0
(1..100).each do |i|
if (i % 3 != 0) && (i % 5 != 0) then
res += i
end
end
puts res
# ref: http://tenka1-2016-quala.contest.atcoder.jp/submissions/827678
node = Struct.new("Node", :parent, :children, :value)
n, m = gets.chomp.split(" ").map(&:to_i)
@graph = Array.new(n){node.new(-1, [], 0)}
1.upto(n-1).each do |i|
parent = gets.chomp.to_i
@graph[i][:parent] = parent
@graph[parent][:children] = @graph[parent][:children].push(i)
end
m.times do
b, c = gets.chomp.split(" ").map(&:to_i)
@graph[b][:value] = c
end
def dfs(node)
s = node.children.map{|e| dfs(@graph[e])}
if node.parent == -1 then
y = 0
else
y = node.children.map{|e| @graph[e].value}.min || 0
end
node[:value] = node[:value] + y
return s.inject(node.value){|s, e| s + (e - y)}
end
puts dfs(@graph[0])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment