結果

問題 No.1878 union-find の数え上げ
ユーザー wolgnikwolgnik
提出日時 2022-03-18 22:11:50
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 577 bytes
コンパイル時間 251 ms
コンパイル使用メモリ 82,416 KB
実行使用メモリ 96,268 KB
最終ジャッジ日時 2024-04-14 12:11:13
合計ジャッジ時間 2,343 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 AC 36 ms
53,504 KB
testcase_08 AC 37 ms
52,472 KB
testcase_09 AC 38 ms
52,768 KB
testcase_10 AC 38 ms
53,492 KB
testcase_11 AC 37 ms
52,580 KB
testcase_12 AC 37 ms
53,116 KB
testcase_13 WA -
testcase_14 AC 96 ms
96,268 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline
N = int(input())
mod = 998244353

e = [[] for _ in range(N + 1)]
for x in range(2, N + 1):
  p = int(input())
  e[p].append(x)

s = [1]
vis = [0] * (N + 1)
parent = [0] * (N + 1)
vis[1] = 1
order = []
while len(s):
  x = s.pop()
  order.append(x)
  for y in e[x]:
    if vis[y]: continue
    parent[y] = x
    vis[y] = 1
    s.append(y)

order.reverse()
dp = [1] * (N + 1)
for x in order:
  for y in e[x]:
    if parent[x] == y: continue
    dp[x] *= dp[y]
    dp[x] %= mod
  if parent[x] > 1:
    dp[x] += 1
    dp[x] %= mod

print(dp[1])
0