結果

問題 No.758 VVVVV
ユーザー 双六双六
提出日時 2020-08-27 21:51:34
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 215 ms / 2,000 ms
コード長 1,495 bytes
コンパイル時間 210 ms
コンパイル使用メモリ 82,520 KB
実行使用メモリ 97,180 KB
最終ジャッジ日時 2024-04-25 21:48:20
合計ジャッジ時間 4,602 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 40 ms
55,052 KB
testcase_01 AC 41 ms
54,452 KB
testcase_02 AC 40 ms
55,584 KB
testcase_03 AC 203 ms
96,388 KB
testcase_04 AC 117 ms
85,028 KB
testcase_05 AC 100 ms
83,628 KB
testcase_06 AC 99 ms
83,244 KB
testcase_07 AC 199 ms
95,828 KB
testcase_08 AC 121 ms
86,824 KB
testcase_09 AC 81 ms
79,572 KB
testcase_10 AC 106 ms
84,476 KB
testcase_11 AC 211 ms
96,456 KB
testcase_12 AC 93 ms
81,976 KB
testcase_13 AC 99 ms
84,064 KB
testcase_14 AC 85 ms
80,572 KB
testcase_15 AC 78 ms
78,280 KB
testcase_16 AC 76 ms
78,688 KB
testcase_17 AC 180 ms
93,676 KB
testcase_18 AC 113 ms
84,736 KB
testcase_19 AC 204 ms
96,728 KB
testcase_20 AC 95 ms
83,036 KB
testcase_21 AC 96 ms
83,276 KB
testcase_22 AC 215 ms
97,180 KB
testcase_23 AC 40 ms
54,692 KB
testcase_24 AC 39 ms
54,516 KB
testcase_25 AC 39 ms
54,368 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys; input = sys.stdin.buffer.readline
sys.setrecursionlimit(10**7)
from collections import defaultdict
con = 10 ** 9 + 7; INF = float("inf")

def getlist():
	return list(map(int, input().split()))

class Combination(object):
	def __init__(self, N, con):
		self.fac = [0] * (N + 1)
		self.inv = [0] * (N + 1)
		self.finv = [0] * (N + 1)
		self.fac[0], self.fac[1] = 1, 1
		self.inv[1] = 1
		self.finv[0], self.finv[1] = 1, 1

		for i in range(2, N + 1):
			self.fac[i] = self.fac[i - 1] * i % con
			self.inv[i] = self.inv[con % i] * (con - (con // i)) % con
			self.finv[i] = self.finv[i - 1] * self.inv[i] % con

	def com(self, N, k):
		return (self.fac[N] * self.finv[k] * self.finv[N - k]) % con

class Graph(object):
	def __init__(self):
		self.graph = defaultdict(list)

	def __len__(self):
		return len(self.graph)

	def add_edge(self, a, b):
		self.graph[a].append(b)

def DFS(G, W, visit, node):
	for i in G.graph[node]:
		if visit[i] != 1:
			visit[i] = 1
			DFS(G, W, visit, i)
			W[node] += W[i]

#処理内容
def main():
	N = int(input())
	G = Graph()
	for i in range(N - 1):
		a, b = getlist()
		a -= 1; b -= 1
		G.add_edge(a, b)
		G.add_edge(b, a)

	W = [1] * N
	visit = [0] * N
	visit[0] = 1
	DFS(G, W, visit, 0)

	#部分木 大きさ
	leaf_cnt = 0
	for i in W:
		if i == 1:
			leaf_cnt += 1

	C = Combination(N, con)
	ans = C.com(N - 1, leaf_cnt - 1) * C.com(N - 2, leaf_cnt - 1) * pow(leaf_cnt, con - 2, con)
	ans %= con
	print(ans)

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