結果

問題 No.758 VVVVV
ユーザー matsu7874matsu7874
提出日時 2018-12-01 20:57:09
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 545 ms / 2,000 ms
コード長 1,185 bytes
コンパイル時間 86 ms
コンパイル使用メモリ 12,672 KB
実行使用メモリ 31,260 KB
最終ジャッジ日時 2024-04-22 02:59:19
合計ジャッジ時間 6,820 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 29 ms
10,880 KB
testcase_01 AC 30 ms
10,880 KB
testcase_02 AC 30 ms
10,880 KB
testcase_03 AC 521 ms
30,220 KB
testcase_04 AC 236 ms
19,708 KB
testcase_05 AC 181 ms
17,180 KB
testcase_06 AC 166 ms
16,596 KB
testcase_07 AC 514 ms
29,812 KB
testcase_08 AC 266 ms
20,604 KB
testcase_09 AC 111 ms
14,336 KB
testcase_10 AC 193 ms
17,656 KB
testcase_11 AC 535 ms
30,724 KB
testcase_12 AC 139 ms
15,744 KB
testcase_13 AC 179 ms
17,192 KB
testcase_14 AC 105 ms
14,208 KB
testcase_15 AC 80 ms
13,056 KB
testcase_16 AC 87 ms
13,440 KB
testcase_17 AC 438 ms
27,720 KB
testcase_18 AC 241 ms
19,728 KB
testcase_19 AC 523 ms
30,604 KB
testcase_20 AC 151 ms
16,256 KB
testcase_21 AC 171 ms
16,768 KB
testcase_22 AC 545 ms
31,260 KB
testcase_23 AC 30 ms
10,880 KB
testcase_24 AC 29 ms
10,880 KB
testcase_25 AC 30 ms
10,880 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