結果

問題 No.101 ぐるぐる!あみだくじ!
ユーザー lam6er
提出日時 2025-03-20 20:56:27
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 48 ms / 5,000 ms
コード長 993 bytes
コンパイル時間 157 ms
コンパイル使用メモリ 82,712 KB
実行使用メモリ 61,544 KB
最終ジャッジ日時 2025-03-20 20:56:54
合計ジャッジ時間 3,256 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 37
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
from math import gcd

def main():
    N = int(sys.stdin.readline())
    K = int(sys.stdin.readline())
    lines = []
    for _ in range(K):
        X, Y = map(int, sys.stdin.readline().split())
        lines.append((X, Y))
    
    # Compute permutation after one shuffle
    perm = list(range(N + 1))  # 1-based indexing
    for i in range(1, N + 1):
        pos = i
        for X, Y in lines:
            if pos == X:
                pos = Y
            elif pos == Y:
                pos = X
        perm[i] = pos
    
    # Find cycle lengths and compute LCM
    visited = [False] * (N + 1)
    S = 1
    for i in range(1, N + 1):
        if not visited[i]:
            cycle_length = 0
            current = i
            while not visited[current]:
                visited[current] = True
                current = perm[current]
                cycle_length += 1
            S = S * cycle_length // gcd(S, cycle_length)
    
    print(S)

if __name__ == "__main__":
    main()
0