Submission #709189


Source Code Expand

n = gets.to_i
$links = Hash.new { |hash, key| hash[key] = [] }
(n - 1).times do
    a, b = gets.split().map { |e| e.to_i - 1 }
    $links[a] = $links[a] << b
    $links[b] = $links[b] << a
end
$w_dp = Array.new(n, 0)
$b_dp = Array.new(n, 0)
$searched = Array.new(n, false)
$mod = 10 ** 9 + 7

def search(i)
    if $w_dp[i] > 0 && $b_dp[i] > 0
        return $w_dp[i], $b_dp[i]
    end
    $searched[i] = true
    white = 1
    black = 1
    $links[i].each do |j|
        if !$searched[j]
            w, b = search(j)
            white *= w + b
            white %= $mod
            black *= w
            black %= $mod
        end
    end
    $w_dp[i] = white
    $b_dp[i] = black
    return white, black
end

w, b = search(0)
puts ((w + b) % $mod)

Submission Info

Submission Time
Task D - 塗り絵
User sorao
Language Ruby (2.3.3)
Score 100
Code Size 782 Byte
Status AC
Exec Time 649 ms
Memory 19836 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 17 ms 1788 KB
001.txt AC 17 ms 1788 KB
002.txt AC 517 ms 17532 KB
003.txt AC 627 ms 19708 KB
004.txt AC 421 ms 14844 KB
005.txt AC 622 ms 19836 KB
006.txt AC 589 ms 18812 KB
007.txt AC 649 ms 19708 KB
008.txt AC 459 ms 16636 KB
009.txt AC 626 ms 19708 KB
010.txt AC 466 ms 16764 KB
011.txt AC 623 ms 19580 KB
012.txt AC 367 ms 12156 KB
013.txt AC 617 ms 19708 KB
014.txt AC 304 ms 10876 KB
015.txt AC 633 ms 19836 KB
016.txt AC 394 ms 12540 KB
017.txt AC 627 ms 19580 KB
018.txt AC 121 ms 5628 KB
019.txt AC 621 ms 19708 KB
020.txt AC 74 ms 3836 KB
021.txt AC 623 ms 19708 KB