結果

問題 No.101 ぐるぐる!あみだくじ!
ユーザー rlangevinrlangevin
提出日時 2023-07-14 12:20:21
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 77 ms / 5,000 ms
コード長 1,248 bytes
コンパイル時間 1,546 ms
コンパイル使用メモリ 86,952 KB
実行使用メモリ 71,400 KB
最終ジャッジ日時 2023-10-14 01:47:15
合計ジャッジ時間 5,416 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 75 ms
71,348 KB
testcase_01 AC 74 ms
71,380 KB
testcase_02 AC 76 ms
71,224 KB
testcase_03 AC 75 ms
71,220 KB
testcase_04 AC 74 ms
70,864 KB
testcase_05 AC 76 ms
71,000 KB
testcase_06 AC 72 ms
71,312 KB
testcase_07 AC 74 ms
71,400 KB
testcase_08 AC 75 ms
70,872 KB
testcase_09 AC 73 ms
71,200 KB
testcase_10 AC 74 ms
71,132 KB
testcase_11 AC 75 ms
71,188 KB
testcase_12 AC 75 ms
71,004 KB
testcase_13 AC 76 ms
71,312 KB
testcase_14 AC 75 ms
71,040 KB
testcase_15 AC 74 ms
71,276 KB
testcase_16 AC 74 ms
71,020 KB
testcase_17 AC 75 ms
71,040 KB
testcase_18 AC 74 ms
71,040 KB
testcase_19 AC 74 ms
71,080 KB
testcase_20 AC 75 ms
71,080 KB
testcase_21 AC 76 ms
71,084 KB
testcase_22 AC 74 ms
70,848 KB
testcase_23 AC 77 ms
71,352 KB
testcase_24 AC 74 ms
71,072 KB
testcase_25 AC 74 ms
71,336 KB
testcase_26 AC 75 ms
71,380 KB
testcase_27 AC 77 ms
71,080 KB
testcase_28 AC 75 ms
71,044 KB
testcase_29 AC 75 ms
71,136 KB
testcase_30 AC 76 ms
71,004 KB
testcase_31 AC 75 ms
71,208 KB
testcase_32 AC 75 ms
71,184 KB
testcase_33 AC 75 ms
71,216 KB
testcase_34 AC 75 ms
71,244 KB
testcase_35 AC 73 ms
71,132 KB
testcase_36 AC 74 ms
71,008 KB
testcase_37 AC 75 ms
71,076 KB
testcase_38 AC 75 ms
71,244 KB
testcase_39 AC 73 ms
71,316 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline

class UnionFind(object):
    def __init__(self, n=1):
        self.par = [i for i in range(n)]
        self.rank = [0 for _ in range(n)]
        self.size = [1 for _ in range(n)]

    def find(self, x):
        if self.par[x] == x:
            return x
        else:
            self.par[x] = self.find(self.par[x])
            return self.par[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x != y:
            if self.rank[x] < self.rank[y]:
                x, y = y, x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1
            self.par[y] = x
            self.size[x] += self.size[y]

    def is_same(self, x, y):
        return self.find(x) == self.find(y)

    def get_size(self, x):
        x = self.find(x)
        return self.size[x]
    
N = int(input())
L = list(range(N))
K = int(input())
for _ in range(K):
    X, Y = map(int, input().split())
    X, Y = X - 1, Y - 1
    L[X], L[Y] = L[Y], L[X]

U = UnionFind(N)
ans = 1

from math import lcm
S = set()
for i in range(N):
    U.union(i, L[i])

for i in range(N):
    if U.find(i) in S:
        continue
    ans = lcm(ans, U.get_size(i))
    S.add(U.find(i))

print(ans)
0