結果
問題 | No.2289 順列ソート |
ユーザー | siman |
提出日時 | 2023-05-06 13:33:20 |
言語 | Ruby (3.3.0) |
結果 |
AC
|
実行時間 | 87 ms / 2,000 ms |
コード長 | 923 bytes |
コンパイル時間 | 146 ms |
コンパイル使用メモリ | 7,424 KB |
実行使用メモリ | 12,288 KB |
最終ジャッジ日時 | 2024-05-03 01:38:34 |
合計ジャッジ時間 | 3,433 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 83 ms
12,160 KB |
testcase_01 | AC | 84 ms
12,288 KB |
testcase_02 | AC | 86 ms
12,288 KB |
testcase_03 | AC | 85 ms
12,160 KB |
testcase_04 | AC | 85 ms
12,160 KB |
testcase_05 | AC | 86 ms
12,160 KB |
testcase_06 | AC | 85 ms
12,160 KB |
testcase_07 | AC | 86 ms
12,160 KB |
testcase_08 | AC | 85 ms
12,288 KB |
testcase_09 | AC | 87 ms
12,160 KB |
testcase_10 | AC | 87 ms
12,160 KB |
testcase_11 | AC | 85 ms
12,160 KB |
testcase_12 | AC | 86 ms
12,032 KB |
testcase_13 | AC | 84 ms
12,160 KB |
testcase_14 | AC | 84 ms
12,160 KB |
testcase_15 | AC | 85 ms
12,160 KB |
testcase_16 | AC | 85 ms
12,288 KB |
testcase_17 | AC | 85 ms
12,160 KB |
testcase_18 | AC | 85 ms
12,160 KB |
testcase_19 | AC | 87 ms
12,288 KB |
testcase_20 | AC | 85 ms
12,288 KB |
testcase_21 | AC | 84 ms
12,032 KB |
testcase_22 | AC | 84 ms
12,288 KB |
testcase_23 | AC | 84 ms
12,160 KB |
コンパイルメッセージ
Syntax OK
ソースコード
class UnionFind def initialize(n) @size = Array.new(n, 1) @rank = Array.new(n, 0) @parent = [] (0..n).each do |i| @parent[i] = i end end def find(x) if @parent[x] == x x else @parent[x] = find(@parent[x]) end end def unite(x, y) x = find(x) y = find(y) return if x == y if @rank[x] < @rank[y] @parent[x] = y @size[y] += @size[x] else @parent[y] = x @size[x] += @size[y] @rank[x] += 1 if @rank[x] == @rank[y] end end def same?(x, y) find(x) == find(y) end def size(x) @size[find(x)] end end N = gets.to_i P = gets.split.map(&:to_i) uf = UnionFind.new(N + 1) P.each.with_index(1) do |v, i| uf.unite(v, i) end visited = Array.new(N + 1, false) ans = 0 1.upto(N) do |i| x = uf.find(i) next if visited[x] visited[x] = true cnt = uf.size(x) ans += (cnt - 1) end puts ans