結果

問題 No.1878 union-find の数え上げ
ユーザー あかしあみどりあかしあみどり
提出日時 2023-03-10 21:39:27
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 264 ms / 2,000 ms
コード長 1,085 bytes
コンパイル時間 158 ms
コンパイル使用メモリ 81,708 KB
実行使用メモリ 103,348 KB
最終ジャッジ日時 2023-10-18 07:23:03
合計ジャッジ時間 3,733 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 179 ms
103,348 KB
testcase_01 AC 260 ms
93,704 KB
testcase_02 AC 262 ms
93,708 KB
testcase_03 AC 264 ms
93,708 KB
testcase_04 AC 244 ms
93,440 KB
testcase_05 AC 116 ms
80,452 KB
testcase_06 AC 117 ms
80,516 KB
testcase_07 AC 42 ms
55,336 KB
testcase_08 AC 42 ms
55,336 KB
testcase_09 AC 43 ms
55,336 KB
testcase_10 AC 43 ms
55,336 KB
testcase_11 AC 43 ms
55,336 KB
testcase_12 AC 43 ms
55,336 KB
testcase_13 AC 42 ms
55,336 KB
testcase_14 AC 164 ms
93,708 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def oi(): return int(input())
def os(): return input()
def mi(): return list(map(int, input().split()))

# import sys
# input = sys.stdin.readline
# import sys
# sys.setrecursionlimit(10**8)
# import pypyjit
# pypyjit.set_param('max_unroll_recursion=-1')
input_count = 0

mod = 998244353
input_count = 0
N = oi()
NODE = N
LOOP_NUM = N-1
maps = {i:[] for i in range(NODE)}
num = 2
for i in range(LOOP_NUM):
    parent = oi()
    maps[num-1].append(parent-1)
    maps[parent-1].append(num-1)
    num += 1
from collections import deque
reached = [-1] * N ## 1つ前のノードを持たせると微妙に遅くなるのでやめよう
deq = deque([[0,0]])

while deq:

    node, num = deq.popleft() #BFS
    reached[node] = num

    for next_node in maps[node]:
        # 訪問済み判定
        if reached[next_node] != -1:
            continue

        reached[next_node] = num+1
        deq.append([next_node,num+1])
        

out = 1
from collections import Counter
for k, v in Counter(reached).items():
    if k==0:
        continue
    out = (out * pow(k, v, mod))%mod
print(out)
0