結果
問題 | No.2289 順列ソート |
ユーザー |
![]() |
提出日時 | 2023-05-05 21:21:22 |
言語 | Python3 (3.13.1 + numpy 2.2.1 + scipy 1.14.1) |
結果 |
AC
|
実行時間 | 32 ms / 2,000 ms |
コード長 | 688 bytes |
コンパイル時間 | 157 ms |
コンパイル使用メモリ | 12,672 KB |
実行使用メモリ | 10,752 KB |
最終ジャッジ日時 | 2024-11-23 05:45:19 |
合計ジャッジ時間 | 1,668 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 21 |
ソースコード
import syssys.setrecursionlimit(500000)class UF:def __init__(self, n):self.root = [i for i in range(n)]self.subt = [1] * ndef find(self, x):if self.root[x] == x:return xelse:self.root[x] = self.find(self.root[x])return self.root[x]def union(self, x, y):x, y = self.find(x), self.find(y)x, y = min(x, y), max(x, y)if x != y:self.subt[x] += self.subt[y]self.root[y] = xdef size(self, x):return self.subt[self.find(x)]n = int(input())p = list(map(lambda x: int(x)-1, input().split()))uf = UF(n)for i in range(n):uf.union(i, p[i])ans = nfor i in range(n):ans -= i == uf.find(i)print(ans)