結果

問題 No.2428 Returning Shuffle
ユーザー titiatitia
提出日時 2023-09-04 01:16:33
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,123 ms / 2,000 ms
コード長 1,018 bytes
コンパイル時間 827 ms
コンパイル使用メモリ 87,012 KB
実行使用メモリ 274,044 KB
最終ジャッジ日時 2023-09-04 01:16:45
合計ジャッジ時間 9,798 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 512 ms
103,172 KB
testcase_01 AC 761 ms
102,264 KB
testcase_02 AC 740 ms
102,752 KB
testcase_03 AC 69 ms
71,344 KB
testcase_04 AC 69 ms
71,264 KB
testcase_05 AC 68 ms
71,348 KB
testcase_06 AC 68 ms
71,348 KB
testcase_07 AC 69 ms
71,136 KB
testcase_08 AC 69 ms
71,240 KB
testcase_09 AC 68 ms
71,420 KB
testcase_10 AC 69 ms
71,436 KB
testcase_11 AC 69 ms
71,532 KB
testcase_12 AC 69 ms
71,632 KB
testcase_13 AC 76 ms
71,364 KB
testcase_14 AC 68 ms
71,224 KB
testcase_15 AC 69 ms
71,604 KB
testcase_16 AC 69 ms
71,348 KB
testcase_17 AC 70 ms
71,608 KB
testcase_18 AC 69 ms
71,236 KB
testcase_19 AC 654 ms
102,808 KB
testcase_20 AC 685 ms
102,988 KB
testcase_21 AC 72 ms
71,360 KB
testcase_22 AC 67 ms
71,480 KB
testcase_23 AC 70 ms
71,604 KB
testcase_24 AC 1,047 ms
274,044 KB
testcase_25 AC 1,123 ms
274,040 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline

mod=998244353

N,M=map(int,input().split())

P=list(range(N))
for i in range(M):
    A=list(map(int,input().split()))[1:]
    X=[]

    for a in A:
        X.append(P[a-1])

    for i in range(len(A)):
        P[A[i]-1]=X[i-1]

    #print(P)

# UnionFind

Group = [i for i in range(N+1)] # グループ分け
Nodes = [1]*(N+1) # 各グループのノードの数

def find(x):
    while Group[x] != x:
        x=Group[x]
    return x

def Union(x,y):
    if find(x) != find(y):
        if Nodes[find(x)] < Nodes[find(y)]:
            
            Nodes[find(y)] += Nodes[find(x)]
            Nodes[find(x)] = 0
            Group[find(x)] = find(y)
            
        else:
            Nodes[find(x)] += Nodes[find(y)]
            Nodes[find(y)] = 0
            Group[find(y)] = find(x)

for i in range(N):
    Union(i,P[i])

from math import gcd

def lcm(x,y):
    return x*y//gcd(x,y)

ANS=1
for i in range(N):
    if find(i)==i:
        ANS=lcm(ANS,Nodes[i])

print(ANS%mod)
0