結果

問題 No.101 ぐるぐる!あみだくじ!
ユーザー 👑 rin204rin204
提出日時 2022-07-14 13:16:17
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 88 ms / 5,000 ms
コード長 1,604 bytes
コンパイル時間 268 ms
コンパイル使用メモリ 87,280 KB
実行使用メモリ 75,848 KB
最終ジャッジ日時 2023-09-08 03:16:21
合計ジャッジ時間 5,355 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 68 ms
71,536 KB
testcase_01 AC 82 ms
75,676 KB
testcase_02 AC 88 ms
75,492 KB
testcase_03 AC 70 ms
71,356 KB
testcase_04 AC 72 ms
71,444 KB
testcase_05 AC 86 ms
75,380 KB
testcase_06 AC 70 ms
71,164 KB
testcase_07 AC 78 ms
71,420 KB
testcase_08 AC 76 ms
71,360 KB
testcase_09 AC 73 ms
71,460 KB
testcase_10 AC 75 ms
71,632 KB
testcase_11 AC 76 ms
71,316 KB
testcase_12 AC 80 ms
71,568 KB
testcase_13 AC 81 ms
75,412 KB
testcase_14 AC 74 ms
71,520 KB
testcase_15 AC 78 ms
71,304 KB
testcase_16 AC 80 ms
75,228 KB
testcase_17 AC 78 ms
71,368 KB
testcase_18 AC 74 ms
71,544 KB
testcase_19 AC 75 ms
71,544 KB
testcase_20 AC 75 ms
71,576 KB
testcase_21 AC 78 ms
71,168 KB
testcase_22 AC 76 ms
71,444 KB
testcase_23 AC 84 ms
75,496 KB
testcase_24 AC 74 ms
71,312 KB
testcase_25 AC 83 ms
75,848 KB
testcase_26 AC 81 ms
75,372 KB
testcase_27 AC 83 ms
75,292 KB
testcase_28 AC 74 ms
71,312 KB
testcase_29 AC 83 ms
75,816 KB
testcase_30 AC 81 ms
75,376 KB
testcase_31 AC 84 ms
75,400 KB
testcase_32 AC 80 ms
71,448 KB
testcase_33 AC 84 ms
75,720 KB
testcase_34 AC 71 ms
71,432 KB
testcase_35 AC 71 ms
71,412 KB
testcase_36 AC 71 ms
71,428 KB
testcase_37 AC 72 ms
71,516 KB
testcase_38 AC 72 ms
71,152 KB
testcase_39 AC 72 ms
71,336 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from math import gcd

class UnionFind:
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n
        self.group = n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return
        self.group -= 1
        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        return -self.parents[self.find(x)]

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

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return self.group

    def all_group_members(self):
        dic = {r:[] for r in self.roots()}
        for i in range(self.n):
            dic[self.find(i)].append(i)
        return dic

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())

n = int(input())
k = int(input())
A = [i for i in range(n)]
for _ in range(k):
    x, y = map(int, input().split())
    x -= 1
    y -= 1
    A[x], A[y] = A[y], A[x]

UF = UnionFind(n)
for i, a in enumerate(A):
    UF.union(i, a)

ans = 1
for r in UF.roots():
    s = UF.size(r)
    ans = ans * s // gcd(ans, s)
print(ans)
0