Submission #1152698


Source Code Expand

# 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))

Submission Info

Submission Time
Task D - Oriented Tree
User dimaqq
Language Python (3.4.3)
Score 1800
Code Size 2668 Byte
Status AC
Exec Time 1101 ms
Memory 24176 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 1800 / 1800
Status
AC × 4
AC × 33
Set Name Test Cases
Sample 0_00.txt, 0_01.txt, 0_02.txt, 0_03.txt
All 0_00.txt, 0_01.txt, 0_02.txt, 0_03.txt, 1_00.txt, 1_01.txt, 1_02.txt, 1_03.txt, 1_04.txt, 1_05.txt, 1_06.txt, 1_07.txt, 1_08.txt, 1_09.txt, 1_10.txt, 1_11.txt, 1_12.txt, 1_13.txt, 1_14.txt, 1_15.txt, 1_16.txt, 1_17.txt, 1_18.txt, 1_19.txt, 1_20.txt, 1_21.txt, 1_22.txt, 1_23.txt, 1_24.txt, 1_25.txt, 1_26.txt, 1_27.txt, 1_28.txt
Case Name Status Exec Time Memory
0_00.txt AC 19 ms 3316 KB
0_01.txt AC 18 ms 3316 KB
0_02.txt AC 18 ms 3192 KB
0_03.txt AC 18 ms 3192 KB
1_00.txt AC 18 ms 3192 KB
1_01.txt AC 559 ms 14412 KB
1_02.txt AC 1101 ms 24176 KB
1_03.txt AC 28 ms 3700 KB
1_04.txt AC 28 ms 3700 KB
1_05.txt AC 34 ms 3828 KB
1_06.txt AC 43 ms 3956 KB
1_07.txt AC 38 ms 3952 KB
1_08.txt AC 51 ms 4272 KB
1_09.txt AC 65 ms 4912 KB
1_10.txt AC 60 ms 4528 KB
1_11.txt AC 131 ms 6252 KB
1_12.txt AC 140 ms 6504 KB
1_13.txt AC 571 ms 14284 KB
1_14.txt AC 519 ms 13788 KB
1_15.txt AC 811 ms 23108 KB
1_16.txt AC 1051 ms 23096 KB
1_17.txt AC 31 ms 3700 KB
1_18.txt AC 31 ms 3696 KB
1_19.txt AC 30 ms 3700 KB
1_20.txt AC 31 ms 3700 KB
1_21.txt AC 33 ms 3572 KB
1_22.txt AC 30 ms 3696 KB
1_23.txt AC 29 ms 3696 KB
1_24.txt AC 28 ms 3696 KB
1_25.txt AC 29 ms 3700 KB
1_26.txt AC 29 ms 3700 KB
1_27.txt AC 30 ms 3700 KB
1_28.txt AC 29 ms 3700 KB