Submission #3777038


Source Code Expand

N = gets.to_i
G = Array.new(N+1){[]}
(2 .. N).each do |child|
  parent = gets.to_i
  G[parent] << child
end
class << G
  def dfs
    salary = Array.new(N+1, 0)
    used = Array.new(N+1)
    stack = [1]
    until stack.empty?
      v = stack.last
      used[v] = 0 unless used[v]
      offset = used[v]
      
      if offset < G[v].size
        u = G[v][offset]
        stack << u
        used[v] += 1
      else
        if G[v].empty?
          salary[v] = 1
        else
          salary[v] = G[v].map{|u| salary[u]}.minmax.inject(&:+) + 1
        end
        stack.pop
      end
    end
    salary[1]
  end
end

puts G.dfs

Submission Info

Submission Time
Task C - 高橋君の給料
User tinsep19
Language Ruby (2.3.3)
Score 100
Code Size 660 Byte
Status AC
Exec Time 8 ms
Memory 1788 KB

Compile Error

./Main.rb:25: warning: shadowing outer local variable - u

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 100 / 100
Status
AC × 4
AC × 22
Set Name Test Cases
Sample example_0.txt, example_1.txt, example_2.txt, example_3.txt
All example_0.txt, example_1.txt, example_2.txt, example_3.txt, maxrand_0.txt, maxrand_1.txt, maxrand_2.txt, random_0.txt, random_1.txt, random_2.txt, random_3.txt, random_4.txt, random_5.txt, random_6.txt, random_7.txt, random_8.txt, random_9.txt, special_0.txt, example_0.txt, example_1.txt, example_2.txt, example_3.txt
Case Name Status Exec Time Memory
example_0.txt AC 8 ms 1788 KB
example_1.txt AC 8 ms 1788 KB
example_2.txt AC 8 ms 1788 KB
example_3.txt AC 8 ms 1788 KB
maxrand_0.txt AC 8 ms 1788 KB
maxrand_1.txt AC 8 ms 1788 KB
maxrand_2.txt AC 8 ms 1788 KB
random_0.txt AC 8 ms 1788 KB
random_1.txt AC 8 ms 1788 KB
random_2.txt AC 8 ms 1788 KB
random_3.txt AC 8 ms 1788 KB
random_4.txt AC 8 ms 1788 KB
random_5.txt AC 8 ms 1788 KB
random_6.txt AC 8 ms 1788 KB
random_7.txt AC 8 ms 1788 KB
random_8.txt AC 8 ms 1788 KB
random_9.txt AC 8 ms 1788 KB
special_0.txt AC 8 ms 1788 KB