Problem p03175 (python)

Fixed submission:
import fileinput
from collections import defaultdict

def longest_path(edges):
    if not edges:
        return 2

    modulo = 10**9 + 7
    graph = defaultdict(list)
    
    min_node = 10**9

    for v_a, v_b in edges:
        graph[v_a].append(v_b)
        graph[v_b].append(v_a)

        min_node = min(min_node, v_a, v_b)
    
    set_count_black = [0] * (len(graph) + 1)
    set_count_white = [0] * (len(graph) + 1)

    node_stack = []
    nodes_on_stack = [True] + [False] * node_count

    node_stack.append([min_node, 0])
    nodes_on_stack[min_node] = True

    while node_stack:
        top_node, last_child = node_stack[-1]
        if top_node in graph and last_child < len(graph[top_node]):
            node_stack[-1][1] += 1
            current_child = graph[top_node][last_child]
            if nodes_on_stack[current_child] == False:
                node_stack.append([current_child, 0])
                nodes_on_stack[current_child] = True
        else:
            node, children = node_stack.pop()
            nodes_on_stack[node] = False

            if (children == 1 and node != min_node) or children == 0:
                set_count_black[node] = 1
                set_count_white[node] = 1
            else:
                bc = 1
                wc = 1
                for child in graph[node]:
                    if nodes_on_stack[child]:
                        continue
                    bc *= set_count_white[child]
                    wc *= (set_count_white[child] + set_count_black[child])
                set_count_white[node] = wc % modulo
                set_count_black[node] = bc % modulo
    return (set_count_white[min_node] + set_count_black[min_node]) % modulo

inp = fileinput.FileInput()

node_count = int(inp.readline())
edges = []

for _ in range(node_count - 1):
    split = inp.readline().split(" ")
    edges.append(
        (int(split[0]), int(split[1]))
    )

print(longest_path(edges))

Buggy submission (verdict: Runtime Error):
import fileinput
from collections import defaultdict

def longest_path(edges):
    modulo = 10**9 + 7
    graph = defaultdict(list)
    for v_a, v_b in edges:
        graph[v_a].append(v_b)
        graph[v_b].append(v_a)
    
    set_count_black = [0] * (len(graph) + 1)
    set_count_white = [0] * (len(graph) + 1)

    node_stack = []
    nodes_on_stack = [True] + [False] * node_count

    node_stack.append([1, 0])
    nodes_on_stack[1] = True

    while node_stack:
        top_node, last_child = node_stack[-1]
        if top_node in graph and last_child < len(graph[top_node]):
            node_stack[-1][1] += 1
            current_child = graph[top_node][last_child]
            if nodes_on_stack[current_child] == False:
                node_stack.append([current_child, 0])
                nodes_on_stack[current_child] = True
        else:
            node, children = node_stack.pop()
            nodes_on_stack[node] = False

            if children == 1 and node != 1:
                set_count_black[node] = 1
                set_count_white[node] = 1
            else:
                bc = 1
                wc = 1
                for child in graph[node]:
                    if nodes_on_stack[child]:
                        continue
                    bc *= set_count_white[child]
                    wc *= (set_count_white[child] + set_count_black[child])
                set_count_white[node] = wc % modulo
                set_count_black[node] = bc % modulo
    return (set_count_white[1] + set_count_black[1]) % modulo

inp = fileinput.FileInput()

node_count = int(inp.readline())
edges = []

for _ in range(node_count - 1):
    split = inp.readline().split(" ")
    edges.append(
        (int(split[0]), int(split[1]))
    )

print(longest_path(edges))

Buggy submission (verdict: Runtime Error):
import fileinput
from collections import defaultdict

def longest_path(edges):
    modulo = 10**9 + 7
    graph = defaultdict(list)
    for v_a, v_b in edges:
        graph[v_a].append(v_b)
        graph[v_b].append(v_a)
    
    set_count_black = [0] * (len(graph) + 1)
    set_count_white = [0] * (len(graph) + 1)

    node_stack = []
    nodes_on_stack = [True] + [False] * node_count

    node_stack.append([1, 0])
    nodes_on_stack[1] = True

    while node_stack:
        top_node, last_child = node_stack[-1]
        if top_node in graph and last_child < len(graph[top_node]):
            node_stack[-1][1] += 1
            current_child = graph[top_node][last_child]
            if nodes_on_stack[current_child] == False:
                node_stack.append([current_child, 0])
                nodes_on_stack[current_child] = True
        else:
            node, children = node_stack.pop()
            nodes_on_stack[node] = False

            if children == 1 and node != 1:
                set_count_black[node] = 1
                set_count_white[node] = 1
            else:
                bc = 1
                wc = 1
                for child in graph[node]:
                    if nodes_on_stack[child]:
                        continue
                    bc *= set_count_white[child]
                    wc *= (set_count_white[child] + set_count_black[child])
                set_count_white[node] = wc % modulo
                set_count_black[node] = bc % modulo
    return set_count_white[1] + set_count_black[1]

inp = fileinput.FileInput()

node_count = int(inp.readline())
edges = []

for _ in range(node_count - 1):
    split = inp.readline().split(" ")
    edges.append(
        (int(split[0]), int(split[1]))
    )

print(longest_path(edges))