結果
問題 | No.2289 順列ソート |
ユーザー | yabit |
提出日時 | 2023-06-30 13:51:01 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 97 ms / 2,000 ms |
コード長 | 1,616 bytes |
コンパイル時間 | 611 ms |
コンパイル使用メモリ | 87,088 KB |
実行使用メモリ | 71,820 KB |
最終ジャッジ日時 | 2023-09-21 07:28:39 |
合計ジャッジ時間 | 3,654 ms |
ジャッジサーバーID (参考情報) |
judge13 / judge14 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 94 ms
71,532 KB |
testcase_01 | AC | 94 ms
71,264 KB |
testcase_02 | AC | 94 ms
71,704 KB |
testcase_03 | AC | 97 ms
71,820 KB |
testcase_04 | AC | 95 ms
71,492 KB |
testcase_05 | AC | 96 ms
71,548 KB |
testcase_06 | AC | 95 ms
71,360 KB |
testcase_07 | AC | 94 ms
71,308 KB |
testcase_08 | AC | 95 ms
71,504 KB |
testcase_09 | AC | 95 ms
71,412 KB |
testcase_10 | AC | 96 ms
71,712 KB |
testcase_11 | AC | 95 ms
71,496 KB |
testcase_12 | AC | 94 ms
71,212 KB |
testcase_13 | AC | 95 ms
71,496 KB |
testcase_14 | AC | 95 ms
71,400 KB |
testcase_15 | AC | 95 ms
71,784 KB |
testcase_16 | AC | 97 ms
71,504 KB |
testcase_17 | AC | 95 ms
71,508 KB |
testcase_18 | AC | 95 ms
71,412 KB |
testcase_19 | AC | 97 ms
71,508 KB |
testcase_20 | AC | 96 ms
71,672 KB |
testcase_21 | AC | 95 ms
71,236 KB |
testcase_22 | AC | 95 ms
71,716 KB |
testcase_23 | AC | 95 ms
71,468 KB |
ソースコード
from collections import defaultdict # UnionFind(n) n:頂点数 # parents:i番目の点が属する根 それ自身が根なら-1 # find(x):xの属する根を再帰的に求める # unite(x,y):多い木に小さい木を結合 class UnionFind(): def __init__(self, n): self.n = n self.parents = [-1] * 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 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 len(self.roots()) def all_group_members(self): group_members = defaultdict(list) for member in range(self.n): group_members[self.find(member)].append(member) return group_members def __str__(self): return '\n'.join(f'{r}: {m}' for r, m in self.all_group_members().items()) N = int(input()) P = list(map(int, input().split())) uf = UnionFind(N) for i in range(N): uf.union(P[i]-1, i) print(N - uf.group_count())