python ARC101 E.

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python ARC101 E.相关的知识,希望对你有一定的参考价值。

import sys
from itertools import islice

sys.setrecursionlimit(5001)
MOD = 10 ** 9 + 7

n = int(input())
links = [set() for _ in range(n)]
for line in sys.stdin.readlines():
    x, y = map(int, line.split())
    x -= 1
    y -= 1
    links[x].add(y)
    links[y].add(x)

double_factorial_odd = [0] * (n // 2)
prev = 1
for i in range(n // 2):
    prev = double_factorial_odd[i] = (2 * i + 1) * prev % MOD


def dfs(v, p):
    ret = [0, 1]
    for u in links[v]:
        if u == p:
            continue
        res = dfs(u, v)
        lt, ls = len(ret), len(res)
        mrg = [0] * (lt + ls - 1)
        for i, c in islice(enumerate(ret), 1 - lt % 2, None, 2):
            for j, d in islice(enumerate(res), 1 - ls % 2, None, 2):
                mrg[i + j] = (mrg[i + j] + c * d) % MOD
        ret = mrg

    if len(ret) % 2 == 1:
        ret[0] = -sum(pattern * df % MOD for pattern, df in zip(ret[2::2], double_factorial_odd)) % MOD

    return ret


print(MOD - dfs(0, -1)[0])

以上是关于python ARC101 E.的主要内容,如果未能解决你的问题,请参考以下文章

ARC101F Robots and Exits

arc101比赛记录

[ARC101B]Median of Medians

ARC101FRobots and Exits 树状数组优化DP

Solution -「ARC 101D」「AT4353」Robots and Exits

AtCoder ARC 101 D Median of Medians(逆序对)