Submission #2660793


Source Code Expand

# -*- coding: utf-8 -*-
from sys import setrecursionlimit
setrecursionlimit(1000000)

class Solver():

    def __init__(self):
        N = int(input())
        self.G = [[] for _ in range(N + 1)]
        self.B = [0] * (N+1)
        self.W = [0] * (N+1)
        self.T = [0] * (N+1)
        self.MOD = 10**9 + 7

        for _ in range(N-1):
            a, b = map(int, input().split())
            self.G[a].append(b)
            self.G[b].append(a)

    def total(self, pre, now):
        if self.T[now] == 0:
            self.T[now] = (self.black(pre, now) + self.white(pre, now) )% self.MOD
        return self.T[now]

    def black(self, pre, now):
        if self.B[now] == 0:
            tmp = 1
            for post in self.G[now]:
                if post == pre:
                    continue
                tmp = (tmp * self.white(now, post)) % self.MOD
            self.B[now] = tmp
        return self.B[now]

    def white(self, pre, now):
        if self.W[now] == 0:
            tmp = 1
            for post in self.G[now]:
                if post == pre:
                    continue
                tmp = (tmp * self.total(now, post)) % self.MOD
            self.W[now] = tmp
        return self.W[now]


if __name__ == "__main__":
    S = Solver()
    print(S.total(None, 1))

Submission Info

Submission Time
Task D - 塗り絵
User nadare881
Language Python (3.4.3)
Score 100
Code Size 1342 Byte
Status AC
Exec Time 618 ms
Memory 25564 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 3064 KB
001.txt AC 17 ms 3064 KB
002.txt AC 439 ms 20752 KB
003.txt AC 587 ms 25524 KB
004.txt AC 376 ms 17812 KB
005.txt AC 577 ms 25496 KB
006.txt AC 537 ms 23740 KB
007.txt AC 615 ms 25564 KB
008.txt AC 420 ms 18984 KB
009.txt AC 581 ms 25512 KB
010.txt AC 408 ms 19248 KB
011.txt AC 594 ms 25392 KB
012.txt AC 359 ms 16304 KB
013.txt AC 618 ms 25484 KB
014.txt AC 268 ms 13612 KB
015.txt AC 581 ms 25472 KB
016.txt AC 368 ms 16800 KB
017.txt AC 567 ms 25520 KB
018.txt AC 110 ms 7028 KB
019.txt AC 599 ms 25440 KB
020.txt AC 68 ms 5236 KB
021.txt AC 571 ms 25520 KB