結果

問題 No.758 VVVVV
ユーザー matsu7874matsu7874
提出日時 2018-12-01 20:57:09
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 459 ms / 2,000 ms
コード長 1,185 bytes
コンパイル時間 92 ms
コンパイル使用メモリ 10,888 KB
実行使用メモリ 28,620 KB
最終ジャッジ日時 2023-08-04 04:39:45
合計ジャッジ時間 6,033 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 15 ms
8,340 KB
testcase_01 AC 17 ms
8,176 KB
testcase_02 AC 16 ms
8,212 KB
testcase_03 AC 439 ms
27,636 KB
testcase_04 AC 190 ms
16,940 KB
testcase_05 AC 140 ms
14,500 KB
testcase_06 AC 128 ms
13,808 KB
testcase_07 AC 426 ms
27,184 KB
testcase_08 AC 213 ms
18,000 KB
testcase_09 AC 83 ms
11,788 KB
testcase_10 AC 149 ms
15,016 KB
testcase_11 AC 451 ms
28,212 KB
testcase_12 AC 110 ms
12,992 KB
testcase_13 AC 140 ms
14,552 KB
testcase_14 AC 80 ms
11,520 KB
testcase_15 AC 58 ms
10,456 KB
testcase_16 AC 66 ms
10,764 KB
testcase_17 AC 373 ms
24,864 KB
testcase_18 AC 200 ms
17,080 KB
testcase_19 AC 444 ms
27,904 KB
testcase_20 AC 121 ms
13,420 KB
testcase_21 AC 129 ms
13,868 KB
testcase_22 AC 459 ms
28,620 KB
testcase_23 AC 16 ms
8,332 KB
testcase_24 AC 16 ms
8,280 KB
testcase_25 AC 17 ms
8,260 KB
権限があれば一括ダウンロードができます

ソースコード

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 % mod)


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