結果
問題 | No.2428 Returning Shuffle |
ユーザー |
![]() |
提出日時 | 2023-09-04 01:16:33 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 1,128 ms / 2,000 ms |
コード長 | 1,018 bytes |
コンパイル時間 | 163 ms |
コンパイル使用メモリ | 82,304 KB |
実行使用メモリ | 272,612 KB |
最終ジャッジ日時 | 2024-06-22 01:21:36 |
合計ジャッジ時間 | 9,037 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 23 |
ソースコード
import sysinput = sys.stdin.readlinemod=998244353N,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)# UnionFindGroup = [i for i in range(N+1)] # グループ分けNodes = [1]*(N+1) # 各グループのノードの数def find(x):while Group[x] != x:x=Group[x]return xdef Union(x,y):if find(x) != find(y):if Nodes[find(x)] < Nodes[find(y)]:Nodes[find(y)] += Nodes[find(x)]Nodes[find(x)] = 0Group[find(x)] = find(y)else:Nodes[find(x)] += Nodes[find(y)]Nodes[find(y)] = 0Group[find(y)] = find(x)for i in range(N):Union(i,P[i])from math import gcddef lcm(x,y):return x*y//gcd(x,y)ANS=1for i in range(N):if find(i)==i:ANS=lcm(ANS,Nodes[i])print(ANS%mod)