Problem p03175 (python)

Fixed submission:
# coding=utf-8
# Date: 2020-04-07 00:38

import sys
sys.setrecursionlimit(10**6)

get = lambda: int(raw_input().strip())
getl = lambda: raw_input().strip()
gets = lambda: map(int, raw_input().strip().split())
getss = lambda n: [gets() for _ in xrange(n)]

n = get()
G = [[] for i in xrange(n + 1)]
for i in xrange(n - 1):
    i, j = gets()
    G[i].append(j)
    G[j].append(i)
mod = 10**9 + 7


def dfs(i, pre=0):
    black, white = 1, 1
    for j in G[i]:
        if j == pre: continue
        b, w = dfs(j, i)
        black = black * w % mod
        white = white * (b + w) % mod
    return [black, white]
print sum(dfs(1)) % mod

Buggy submission (verdict: Runtime Error):
# coding=utf-8
# Date: 2020-04-07 00:38

get = lambda: int(raw_input().strip())
getl = lambda: raw_input().strip()
gets = lambda: map(int, raw_input().strip().split())
getss = lambda n: [gets() for _ in xrange(n)]


n = get()
G = [[] for i in xrange(n + 1)]
for i in xrange(n - 1):
    i, j = gets()
    G[i].append(j)
    G[j].append(i)
mod = 10**9 + 7


def dfs(i, pre=0):
    black, white = 1, 1
    for j in G[i]:
        if j == pre: continue
        b, w = dfs(j, i)
        black = black * w % mod
        white = white * (b + w) % mod
    return [black, white]
print sum(dfs(1)) % mod

Buggy submission (verdict: Runtime Error):
# coding=utf-8
# Date: 2020-04-07 00:38

get = lambda: int(raw_input().strip())
getl = lambda: raw_input().strip()
gets = lambda: map(int, raw_input().strip().split())
getss = lambda n: [gets() for _ in xrange(n)]

# fi = open('sample4.in', 'r')
# raw_input = fi.readline

n = get()
G = [set() for i in xrange(n + 1)]
for i in xrange(n - 1):
    i, j = gets()
    G[i].add(j)
    G[j].add(i)
mod = 10**9 + 7


def dfs(i):
    G[i], A = None, G[i]
    black, white = 1, 1
    for j in A:
        G[j].remove(i)
        b, w = dfs(j)
        black = black * w % mod
        white = white * (b + w) % mod
    return [black, white]
print sum(dfs(1))

Buggy submission (verdict: Runtime Error):
# coding=utf-8
# Date: 2020-04-07 00:38

get = lambda: int(raw_input().strip())
getl = lambda: raw_input().strip()
gets = lambda: map(int, raw_input().strip().split())
getss = lambda n: [gets() for _ in xrange(n)]

n = get()
G = [set() for i in xrange(n + 1)]
for i in xrange(n - 1):
    i, j = gets()
    G[i].add(j)
    G[j].add(i)


def dfs(i):
    G[i], A = None, G[i]
    black, white = 1, 1
    for j in A:
        G[j].remove(i)
        b, w = dfs(j)
        black *= w
        white *= b + w
    return [black, white]
print sum(dfs(1))

Buggy submission (verdict: Runtime Error):
# coding=utf-8
# Date: 2020-04-07 00:38

get = lambda: int(raw_input().strip())
getl = lambda: raw_input().strip()
gets = lambda: map(int, raw_input().strip().split())
getss = lambda n: [gets() for _ in xrange(n)]

fi = open('sample4.in', 'r')
raw_input = fi.readline

n = get()
G = [set() for i in xrange(n + 1)]
for i in xrange(n - 1):
    i, j = gets()
    G[i].add(j)
    G[j].add(i)


def dfs(i):
    G[i], A = None, G[i]
    black, white = 1, 1
    for j in A:
        G[j].remove(i)
        b, w = dfs(j)
        black *= w
        white *= b + w
    return [black, white]
print sum(dfs(1))