結果
問題 | No.101 ぐるぐる!あみだくじ! |
ユーザー |
![]() |
提出日時 | 2020-12-26 23:57:04 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 43 ms / 5,000 ms |
コード長 | 1,233 bytes |
コンパイル時間 | 1,229 ms |
コンパイル使用メモリ | 82,020 KB |
実行使用メモリ | 55,860 KB |
最終ジャッジ日時 | 2024-09-25 03:53:43 |
合計ジャッジ時間 | 2,877 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 37 |
ソースコード
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)