Submission #1674545


Source Code Expand

n, x = gets.chomp.split(" ").map(&:to_i)
node = Struct.new("Node", :to)
@h = [0] + gets.chomp.split(" ").map(&:to_i)
@g = Array.new(n+1){node.new([])}
(n-1).times do
  a, b = gets.chomp.split(" ").map(&:to_i)
  @g[a].to << b
  @g[b].to << a
end

@par = Array.new(n+1, -1)
cand = [x]
@par[x] = 0
while true
  break if cand.empty?
  v = cand.pop
  @g[v].to.each do |i|
    if @par[i] == -1
      @par[i] = v
      cand << i
    end
  end
end

def jewel?(v)
  if @h[v] == 1
    return true
  end
  @g[v].to.each do |i|
    next if i == @par[v]
    if @h[i] == 1
      return true
    else
      jewel?(i)
    end
  end
  return false
end


visited = Array.new(n+1, false)
visited[x] = true
cand = [x]
@cost = 0
while true
  break if cand.empty?
  v = cand.pop
  @g[v].to.each do |i|
    if !visited[i] && jewel?(i)
      cand << i
      @cost += 2
    end
    visited[i] = true
  end
end

puts @cost

Submission Info

Submission Time
Task B - ツリーグラフ
User wtnk0812
Language Ruby (2.3.3)
Score 0
Code Size 952 Byte
Status WA
Exec Time 9 ms
Memory 3836 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 0 / 100
Status
AC × 2
AC × 9
WA × 11
Set Name Test Cases
Sample subtask0_sample_01.txt, subtask0_sample_02.txt
All subtask0_sample_01.txt, subtask0_sample_02.txt, subtask1_line01.txt, subtask1_line02.txt, subtask1_line03.txt, subtask1_line04.txt, subtask1_line05.txt, subtask1_line06.txt, subtask1_random01.txt, subtask1_random02.txt, subtask1_random03.txt, subtask1_random04.txt, subtask1_random05.txt, subtask1_random06.txt, subtask1_random07.txt, subtask1_random08.txt, subtask1_special01.txt, subtask1_special02.txt, subtask1_special03.txt, subtask1_special04.txt
Case Name Status Exec Time Memory
subtask0_sample_01.txt AC 8 ms 1788 KB
subtask0_sample_02.txt AC 8 ms 1788 KB
subtask1_line01.txt AC 8 ms 1788 KB
subtask1_line02.txt WA 8 ms 1788 KB
subtask1_line03.txt WA 8 ms 1788 KB
subtask1_line04.txt WA 8 ms 1788 KB
subtask1_line05.txt AC 8 ms 1788 KB
subtask1_line06.txt AC 8 ms 1916 KB
subtask1_random01.txt WA 9 ms 3836 KB
subtask1_random02.txt WA 8 ms 1788 KB
subtask1_random03.txt WA 8 ms 1788 KB
subtask1_random04.txt WA 8 ms 1788 KB
subtask1_random05.txt WA 8 ms 1788 KB
subtask1_random06.txt WA 8 ms 1788 KB
subtask1_random07.txt WA 8 ms 1788 KB
subtask1_random08.txt WA 8 ms 1788 KB
subtask1_special01.txt AC 8 ms 1788 KB
subtask1_special02.txt AC 7 ms 1788 KB
subtask1_special03.txt AC 8 ms 1788 KB
subtask1_special04.txt AC 8 ms 1788 KB