結果

問題 No.101 ぐるぐる!あみだくじ!
ユーザー tktk_snsntktk_snsn
提出日時 2020-12-26 23:57:04
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 43 ms / 5,000 ms
コード長 1,233 bytes
コンパイル時間 181 ms
コンパイル使用メモリ 81,708 KB
実行使用メモリ 55,576 KB
最終ジャッジ日時 2023-10-25 08:46:55
合計ジャッジ時間 3,392 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 39 ms
53,528 KB
testcase_01 AC 42 ms
53,528 KB
testcase_02 AC 41 ms
53,528 KB
testcase_03 AC 40 ms
53,528 KB
testcase_04 AC 40 ms
53,528 KB
testcase_05 AC 42 ms
53,528 KB
testcase_06 AC 40 ms
53,528 KB
testcase_07 AC 42 ms
53,528 KB
testcase_08 AC 41 ms
53,528 KB
testcase_09 AC 41 ms
53,528 KB
testcase_10 AC 41 ms
53,528 KB
testcase_11 AC 41 ms
53,528 KB
testcase_12 AC 42 ms
53,528 KB
testcase_13 AC 42 ms
53,528 KB
testcase_14 AC 41 ms
53,528 KB
testcase_15 AC 41 ms
53,528 KB
testcase_16 AC 42 ms
53,528 KB
testcase_17 AC 42 ms
53,528 KB
testcase_18 AC 41 ms
53,528 KB
testcase_19 AC 42 ms
53,528 KB
testcase_20 AC 41 ms
53,528 KB
testcase_21 AC 41 ms
53,528 KB
testcase_22 AC 41 ms
53,528 KB
testcase_23 AC 42 ms
55,576 KB
testcase_24 AC 40 ms
53,528 KB
testcase_25 AC 42 ms
53,528 KB
testcase_26 AC 42 ms
55,576 KB
testcase_27 AC 42 ms
55,576 KB
testcase_28 AC 41 ms
53,528 KB
testcase_29 AC 43 ms
55,576 KB
testcase_30 AC 41 ms
53,528 KB
testcase_31 AC 41 ms
53,528 KB
testcase_32 AC 41 ms
53,528 KB
testcase_33 AC 42 ms
55,576 KB
testcase_34 AC 39 ms
53,528 KB
testcase_35 AC 40 ms
53,528 KB
testcase_36 AC 39 ms
53,528 KB
testcase_37 AC 40 ms
53,528 KB
testcase_38 AC 40 ms
53,528 KB
testcase_39 AC 40 ms
53,528 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from math import gcd
import sys
input = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 7)


class UF_tree:
    def __init__(self, n):
        self.root = [-1] * (n + 1)
        self.rank = [0] * (n + 1)

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

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

    def unite(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x == y:
            return
        elif self.rank[x] < self.rank[y]:
            self.root[y] += self.root[x]
            self.root[x] = y
        else:
            self.root[x] += self.root[y]
            self.root[y] = x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1

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


N = int(input())
*num, = range(N)

K = int(input())
for _ in range(K):
    x, y = map(int, input().split())
    y -= 2
    num[x], num[y] = num[y], num[x]

uf = UF_tree(N)
for a, b in enumerate(num):
    uf.unite(a, b)

ans = 1
for i in range(N):
    sz = uf.size(i)
    g = gcd(ans, sz)
    ans = ans // g * sz
print(ans)
0