Last active
August 5, 2016 07:52
-
-
Save whatalnk/f3e2c1b0b1667590c64c72882099f21b to your computer and use it in GitHub Desktop.
天下一プログラマーコンテスト2016 [AtCoder] [Ruby]
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
res = 0 | |
(1..100).each do |i| | |
if (i % 3 != 0) && (i % 5 != 0) then | |
res += i | |
end | |
end | |
puts res |
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
# 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