Problem p03794 (python)

Fixed submission:
# doc: git.io/vy4co
def graph(inp):
    nodes = dict()
    N = None
    for line in inp.splitlines():
        if N is None:
            N = int(line.strip())
            for k in range(1, N + 1):
                nodes[k] = set()
            continue
        i, k = map(int, line.split())
        nodes[i].add(k)
        nodes[k].add(i)
    return nodes

def trace(nodes, start, exclude=None):
    return (start,) + max([trace(nodes, n, exclude=start) for n in nodes[start] if n != exclude], key=len, default=())

def tree(nodes, start, exclude=None):
    return tup([tree(nodes, n, exclude=start) for n in nodes[start] if n != exclude])

class tup(tuple):
    def __new__(cls, arg=()):
        rv = super().__new__(cls, arg)
        rv.height = (1 + min((t.height[0] for t in rv), default=-1),
                     1 + max((t.height[1] for t in rv), default=-1))
        rv.edges = len(rv) + sum(t.edges for t in rv)
        return rv

def combinations(nodes):
    path = trace(nodes, trace(nodes, next(iter(nodes)))[-1])
    D = len(path)
    C = D // 2
    root = path[D // 2]
    if D % 2:
        thetree = tree(nodes, root)
        return sum(enum(limits, thetree) for limits in zip(range(C + 1), reversed(range(C + 1))))
    else:
        left = path[D // 2 - 1]
        left_tree = tup([tree(nodes, left, exclude=root)])
        right_tree = tree(nodes, root, exclude=left)
        lg = [i // 2 for i in range(1, C * 2 + 2)]
        ll = list(zip(lg, reversed(lg)))
        rg = [i // 2 for i in range(C * 2 + 1)]
        rl = list(zip(rg, reversed(rg)))
        tot = 0
        for i in range(len(ll)):
            left_limits = ll[i]
            right_limits = rl[i]
            lrv = enum(left_limits, left_tree) - sum(enum(ne, left_tree) for ne in ll[i - 1: i] + ll[i + 1: i + 2])\
                if sum(left_limits) > C else enum(left_limits, left_tree)
            rrv = enum(right_limits, right_tree)
            tot += lrv * rrv
        return tot

def enum(limits, shape, _cache=dict()):
    limits = tuple(sorted(limits))
    r, b = limits
    low, high = shape.height

    if r >= high:
        return 2 ** shape.edges

    if 0 in limits:
        return 1

    key = hash((r, b, shape))
    if key not in _cache:
        tot = 1
        for subtree in shape:
            acc = 0
            for sublimit in ((r - 1, b), (r, b - 1)):
                acc += enum(sublimit, subtree)
            tot *= acc
        _cache[key] = tot
    return _cache[key]

import sys
sys.setrecursionlimit(99999)
g = graph(sys.stdin.read())
rv = combinations(g)
print(rv % (10 ** 9 + 7))

Buggy submission (verdict: Runtime Error):
def build_graph(inp):
    nodes = dict()
    names = dict()
    N = None

    for line in inp:
        # special handling for first line of input -- seed vertices
        if N is None:
            N = int(line.strip())
            for k in range(1, N + 1):
                nodes[k] = set()
                names[id(nodes[k])] = nodes[k]
            continue

        # create edges
        i, k = map(int, line.split())
        nodes[i].add(k)
        nodes[k].add(i)

    return nodes, names


def longest_trace_from(nodes, start, exclude=None):
    traces = [longest_trace_from(nodes, n, exclude=start) for n in nodes[start] if n is not exclude] + [()]
    return (start,) + max(traces, key=len)


def longest_path(nodes):
    """ Find a longest path in the graph:
        Start with a random node, and find a longest trace from there.
        Re-start from the end of that trace and find a lognest trace.
        This will be the longest path in a graph, if graph is a tree. """
    if not nodes:
        return ()
    random = next(iter(nodes))
    start = longest_trace_from(nodes, random)[-1]
    return longest_trace_from(nodes, start)


def shape(nodes, start, exclude=None):
    parts = [shape(nodes, n, exclude=start) for n in nodes[start] if n is not exclude]
    return tuple(sorted(parts))  # any stable order will do


def shape2(nodes, start, exclude=frozenset()):
    parts = [shape(nodes, n, exclude=exclude.union([start])) for n in nodes[start] if n not in exclude]
    return tuple(sorted(parts))  # any stable order will do


def subtree_limits(path):
    L = len(path)
    excl = [frozenset(path[:i][-1:] + path[i + 1:][:1]) for i in range(L)]
    high = [min(i, L - i - 1) for i in range(L)]
    lims = [tuple((H - i, i) for i in range(H // 2 + 1)) for H in high]
    return tuple(dict(start=path[i], exclude=excl[i], limits=lims[i]) for i in range(L))


def combinations(nodes):
    rv = 2
    cache = dict()
    path = longest_path(nodes)
    for d in subtree_limits(path):
        this = d["start"]
        sh = shape2(nodes, d["start"], exclude=d["exclude"])
        for l, r in d["limits"]:
            if not l or not r:
                break  # left == 0 means all right and vv.
        # NOT FINISHED


import pytest
import pprint


def test_sl():
    # pprint.pprint(subtree_limits(tuple(range(9))))  # manual check
    pass


@pytest.fixture
def sample_data1():
    data = """4
              1 2
              1 3
              1 4""".strip().splitlines()
    return build_graph(data)


@pytest.fixture
def nodes(sample_data1):
    nodes, name = sample_data1
    return nodes


def test_com(nodes):
    combinations(nodes)


def test_build(nodes):
    # print(nodes)  # visual inspection
    pass


def test_ltf(nodes):
    assert len(longest_trace_from(nodes, 1)) == 2  # e.g. (1, 2)
    assert len(longest_trace_from(nodes, 2)) == 3  # e.g. (2, 1, 3)


def test_lp(nodes):
    assert len(longest_path(nodes)) == 3  # e.g. (2, 1, 3)