結果
| 問題 | No.101 ぐるぐる!あみだくじ! | 
| コンテスト | |
| ユーザー |  maspy | 
| 提出日時 | 2020-03-05 10:36:06 | 
| 言語 | Python3 (3.13.1 + numpy 2.2.1 + scipy 1.14.1) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 31 ms / 5,000 ms | 
| コード長 | 700 bytes | 
| コンパイル時間 | 78 ms | 
| コンパイル使用メモリ | 12,672 KB | 
| 実行使用メモリ | 11,008 KB | 
| 最終ジャッジ日時 | 2024-10-14 00:51:18 | 
| 合計ジャッジ時間 | 2,484 ms | 
| ジャッジサーバーID (参考情報) | judge3 / judge2 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 37 | 
ソースコード
#!/usr/bin/env python3
# %%
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
from math import gcd
from functools import reduce
# %%
N = int(readline())
K = int(readline())
m = map(int, read().split())
XY = zip(m, m)
# %%
P = list(range(N))
for x, y in XY:
    x -= 1
    y -= 1
    P[x], P[y] = P[y], P[x]
# %%
comp = [0] * N
size = []
n = 0
for i in range(N):
    if comp[i]:
        continue
    n += 1
    comp[i] = n
    s = 1
    while True:
        i = P[i]
        if comp[i]:
            break
        comp[i] = n
        s += 1
    size.append(s)
# %%
lcm = reduce(lambda x, y: x * (y // gcd(x, y)), size)
print(lcm)
            
            
            
        