結果
問題 |
No.1878 union-find の数え上げ
|
ユーザー |
![]() |
提出日時 | 2022-03-18 22:01:32 |
言語 | PyPy3 (7.3.15) |
結果 |
WA
|
実行時間 | - |
コード長 | 676 bytes |
コンパイル時間 | 291 ms |
コンパイル使用メモリ | 82,116 KB |
実行使用メモリ | 97,160 KB |
最終ジャッジ日時 | 2024-10-03 09:34:59 |
合計ジャッジ時間 | 2,474 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 5 WA * 8 |
ソースコード
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() dp0 = [1] * (N + 1) dp1 = [1] * (N + 1) for x in order: for y in e[x]: if parent[x] == y: continue dp0[x] *= dp0[y] dp0[x] %= mod dp1[x] *= dp0[y] + dp1[y] dp1[x] %= mod if parent[x] <= 1: dp1[x] -= dp0[x] dp1[x] %= mod print((dp0[1] + dp1[1]) % mod)