結果

問題 No.758 VVVVV
ユーザー matsu7874matsu7874
提出日時 2018-12-01 21:00:37
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
RE  
実行時間 -
コード長 1,180 bytes
コンパイル時間 434 ms
コンパイル使用メモリ 11,000 KB
実行使用メモリ 28,764 KB
最終ジャッジ日時 2023-09-09 07:12:38
合計ジャッジ時間 5,886 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
8,160 KB
testcase_01 AC 16 ms
8,296 KB
testcase_02 AC 16 ms
8,248 KB
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
testcase_21 RE -
testcase_22 RE -
testcase_23 AC 17 ms
8,224 KB
testcase_24 AC 16 ms
7,804 KB
testcase_25 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

# mod = 10**9 + 7

def combination(n, r):
    assert 0 <= r <= n
    if n - r < r:
        r = n - r
    if r == 0:
        return 1
    if r == 1:
        return n

    numerator = [n - r + i + 1 for i in range(r)]
    denominator = [i + 1 for i in range(r)]

    for p in range(2, r + 1):
        pivot = denominator[p - 1]
        if pivot > 1:
            offset = (n - r) % p
            for i in range(p - 1, r, p):
                numerator[i - offset] //= pivot
                denominator[i] //= pivot

    result = 1
    for i in range(r):
        if numerator[i] > 1:
            result *= numerator[i]
    return result


def main():
    n = int(input())
    assert 1 <= n <= 10**5

    graph = [[] for _ in range(n)]
    for _ in range(n - 1):
        a, b = map(int, input().split())
        assert 1 <= a <= n
        assert 1 <= b <= n
        graph[a - 1].append(b - 1)
        graph[b - 1].append(a - 1)

    if n == 1:
        print(1)
        exit()

    leaf_cnt = len(list(filter(lambda x: len(x) == 1, graph[1:])))

    print(combination(n - 1, leaf_cnt - 1) *
          combination(n - 2, leaf_cnt - 1) // leaf_cnt)


if __name__ == "__main__":
    main()
0