結果

問題 No.101 ぐるぐる!あみだくじ!
コンテスト
ユーザー ayaoni
提出日時 2020-12-15 16:36:11
言語 PyPy3
(7.3.17)
コンパイル:
pypy3 -mpy_compile _filename_
実行:
pypy3 _filename_
結果
AC  
実行時間 29 ms / 5,000 ms
コード長 806 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 153 ms
コンパイル使用メモリ 85,248 KB
実行使用メモリ 55,504 KB
最終ジャッジ日時 2026-04-08 14:14:24
合計ジャッジ時間 2,539 ms
ジャッジサーバーID
(参考情報)
judge1_1 / judge2_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 37
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

import sys
from math import gcd
sys.setrecursionlimit(10**7)
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return map(int,sys.stdin.readline().rstrip().split())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def LI2(): return list(map(int,sys.stdin.readline().rstrip()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())
def LS2(): return list(sys.stdin.readline().rstrip())


N,K = I(),I()

A = [i for i in range(N+1)]
for _ in range(K):
    X,Y = MI()
    A[X],A[Y] = A[Y],A[X]

flag = [0]*(N+1)
ans = 1
for i in range(1,N+1):
    if flag[i]:
        continue
    flag[i] = 1
    r = 1
    a = A[i]
    while a != i:
        r += 1
        flag[a] = 1
        a = A[a]
    ans = ans*r//gcd(ans,r)

print(ans)
0