Problem p03175 (python)

Fixed submission:
import sys
sys.setrecursionlimit(10**9)
mod = 10**9+7
N = int(input())
G = [[] for i in range(N)]
for i in range(N-1):
    x, y = map(int, input().split())
    G[x-1].append(y-1)
    G[y-1].append(x-1)
    
def dfs(i, pi):
    b = 1
    w = 1
    for ni in G[i]:
        if ni == pi:
            continue
        [nb, nw] = dfs(ni, i)
        b *= nw
        b %= mod
        w *= nw + nb
        w %= mod
    return [b, w]

print(sum(dfs(0, -1))%mod)

Buggy submission (verdict: Runtime Error):
mod = 10**9+7
N = int(input())
G = [[] for i in range(N)]
for i in range(N-1):
    x, y = map(int, input().split())
    G[x-1].append(y-1)
    G[y-1].append(x-1)
    
def dfs(i, pi):
    b, w = 1, 1
    for ni in G[i]:
        if ni == pi:
            continue
        nb, nw = dfs(ni, i)
        b *= nw
        b %= mod
        w *= nw + nb
        w %= mod
    return b, w

print(sum(dfs(0, -1))%mod)

Buggy submission (verdict: Runtime Error):
from collections import defaultdict
mod = 10**9+7
N = int(input())
d = defaultdict(list)
for i in range(N-1):
    x, y = map(int, input().split())
    d[x].append(y)
    d[y].append(x)
    
def dfs(i, pi):
    b, w = 1, 1
    for ni in d[i]:
        if ni == pi:
            continue
        nb, nw = dfs(ni, i)
        b *= nw
        b %= mod
        w *= nw + nb
        w %= mod
    return b, w

print(sum(dfs(1, -1))%mod)

Buggy submission (verdict: Runtime Error):
from collections import defaultdict
mod = 10**9+7
N = int(input())
d = defaultdict(list)
for i in range(N-1):
    x, y = map(int, input().split())
    d[x-1].append(y-1)
    d[y-1].append(x-1)
    
def dfs(i, pi):
    b, w = 1, 1
    for ni in d[i]:
        if ni == pi:
            continue
        nb, nw = dfs(ni, i)
        b = b*nw%mod
        w = w*(nw+nb)%mod
    return b, w

print(sum(dfs(0, -1))%mod)

Buggy submission (verdict: Runtime Error):
from collections import defaultdict
mod = 10**9+7
N = int(input())
d = defaultdict(list)
for i in range(N-1):
    x, y = map(int, input().split())
    d[x-1].append(y-1)
    d[y-1].append(x-1)
    
def dfs(i, pi):
    b, w = 1, 1
    for ni in d[i]:
        if ni == pi:
            continue
        nb, nw = dfs(ni, i)
        b = b*nw%mod
        w = w*(nw+nb)%mod
    return b, w

print(sum(dfs(0, -1)))

Buggy submission (verdict: Runtime Error):
from collections import defaultdict
mod = 10**9+7
N = int(input())
d = defaultdict(list)
for i in range(N-1):
    x, y = map(int, input().split())
    d[x-1].append(y-1)
    d[y-1].append(x-1)
    
def f(i):
    if flag[i]:
        return
    flag[i] = 1
    dp[i][0] = 1 #white
    dp[i][1] = 1 #black
    for j in d[i]:
        if not flag[j]:
            f(j)
            dp[i][0] = dp[i][0]*(dp[j][0] + dp[j][1])%mod
            dp[i][1] = dp[i][1]*dp[j][0]%mod
flag = [0] * N
dp = [[0,0] for i in range(N)]
f(0)
print(dp[0][0]+dp[0][1])

Buggy submission (verdict: Runtime Error):
from collections import defaultdict
mod = 10**9+7
N = int(input())
d = defaultdict(list)
for i in range(N-1):
    x, y = map(int, input().split())
    d[x-1].append(y-1)
    d[y-1].append(x-1)
    
def f(i):
    if flag[i]:
        return
    flag[i] = 1
    dp[i][0] = 1 #white
    dp[i][1] = 1 #black
    for j in d[i]:
        if not flag[j]:
            f(j)
            dp[i][0] = dp[i][0]*(dp[j][0] + dp[j][1])%mod
            dp[i][1] = dp[i][1]*dp[j][0]%mod
flag = [0] * N
dp = [[0,0] for i in range(N)]
f(0)
print(dp[0][0]+dp[0][1])