Submission #851597


Source Code Expand

class Node
  attr_reader :children, :value
  def initialize(v)
    @value = v
    @children = []
  end

  def add(other)
    @children << other
  end

  def count_white(from)
    targets = @children.reject { |n| n.value == from }
    @count_white ||=
      if targets.empty?
        1
      else
        targets.map{|n| n.count_white(@value) + n.count_black(@value) }.inject(:*)
      end
  end

  def count_black(from)
    targets = @children.reject { |n| n.value == from }
    @count_black ||= targets.map { |n| n.count_white(@value) }.inject(:*) || 1
  end
end

N = gets.to_i
nodes = Array.new(N) { |i| Node.new(i+1) }
(N-1).times do
  a, b = gets.split.map(&:to_i)
  va, vb = nodes[a-1], nodes[b-1]
  va.add(vb)
  vb.add(va)
end

root = nodes.first
p (root.count_white(root.value) + root.count_black(root.value)) % ((10 ** 9) + 7)

Submission Info

Submission Time
Task D - 塗り絵
User kumojima
Language Ruby (2.3.3)
Score 100
Code Size 871 Byte
Status AC
Exec Time 880 ms
Memory 28028 KB

Judge Result

Set Name All
Score / Max Score 100 / 100
Status
AC × 22
Set Name Test Cases
All 000.txt, 001.txt, 002.txt, 003.txt, 004.txt, 005.txt, 006.txt, 007.txt, 008.txt, 009.txt, 010.txt, 011.txt, 012.txt, 013.txt, 014.txt, 015.txt, 016.txt, 017.txt, 018.txt, 019.txt, 020.txt, 021.txt
Case Name Status Exec Time Memory
000.txt AC 18 ms 1788 KB
001.txt AC 17 ms 1788 KB
002.txt AC 648 ms 23292 KB
003.txt AC 880 ms 26748 KB
004.txt AC 523 ms 20348 KB
005.txt AC 838 ms 26364 KB
006.txt AC 693 ms 24700 KB
007.txt AC 808 ms 27644 KB
008.txt AC 559 ms 21244 KB
009.txt AC 804 ms 27260 KB
010.txt AC 562 ms 20988 KB
011.txt AC 790 ms 25852 KB
012.txt AC 465 ms 20348 KB
013.txt AC 809 ms 28028 KB
014.txt AC 357 ms 13948 KB
015.txt AC 824 ms 27772 KB
016.txt AC 480 ms 20220 KB
017.txt AC 798 ms 26364 KB
018.txt AC 154 ms 7292 KB
019.txt AC 798 ms 28028 KB
020.txt AC 90 ms 4476 KB
021.txt AC 794 ms 26748 KB