結果
| 問題 |
No.1796 木上のクーロン
|
| コンテスト | |
| ユーザー |
gew1fw
|
| 提出日時 | 2025-06-12 16:42:49 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 1,574 bytes |
| コンパイル時間 | 239 ms |
| コンパイル使用メモリ | 82,688 KB |
| 実行使用メモリ | 76,936 KB |
| 最終ジャッジ日時 | 2025-06-12 16:43:09 |
| 合計ジャッジ時間 | 15,346 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 17 TLE * 1 -- * 16 |
ソースコード
import sys
from collections import deque
MOD = 998244353
def main():
input = sys.stdin.read().split()
idx = 0
N = int(input[idx])
idx += 1
Q = list(map(int, input[idx:idx+N]))
idx += N
adj = [[] for _ in range(N+1)]
for _ in range(N-1):
u = int(input[idx])
v = int(input[idx+1])
adj[u].append(v)
adj[v].append(u)
idx += 2
# Precompute inverse squares of 1..N+1
max_d = N
inv_sq = [1] * (max_d + 2)
for d in range(1, max_d + 2):
inv = pow(d, MOD-2, MOD)
inv_sq[d] = (inv * inv) % MOD
# Compute factorial and k0 = (N!)^2 mod MOD
fact = [1] * (N + 1)
for i in range(1, N+1):
fact[i] = fact[i-1] * i % MOD
k0 = fact[N] * fact[N] % MOD
# Initialize E_p for all p
E = [0] * (N + 1)
# For each vertex i, compute distances and accumulate contributions
for i in range(1, N+1):
dist = [-1] * (N + 1)
q = deque([i])
dist[i] = 0
while q:
u = q.popleft()
for v in adj[u]:
if dist[v] == -1:
dist[v] = dist[u] + 1
q.append(v)
# Compute contribution of Q[i-1] to all other vertices
qi = Q[i-1]
for j in range(1, N+1):
d = dist[j]
contrib = qi * inv_sq[d + 1] % MOD
E[j] = (E[j] + contrib) % MOD
# Multiply by k0 and output
for p in range(1, N+1):
res = E[p] * k0 % MOD
print(res)
if __name__ == '__main__':
main()
gew1fw