結果

問題 No.2289 順列ソート
ユーザー chineristACchineristAC
提出日時 2023-05-05 21:21:56
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 114 ms / 2,000 ms
コード長 1,259 bytes
コンパイル時間 278 ms
コンパイル使用メモリ 86,712 KB
実行使用メモリ 74,876 KB
最終ジャッジ日時 2023-08-15 02:42:14
合計ジャッジ時間 4,233 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 112 ms
74,808 KB
testcase_01 AC 113 ms
74,648 KB
testcase_02 AC 113 ms
74,516 KB
testcase_03 AC 112 ms
74,452 KB
testcase_04 AC 109 ms
74,576 KB
testcase_05 AC 112 ms
74,268 KB
testcase_06 AC 113 ms
74,400 KB
testcase_07 AC 114 ms
74,552 KB
testcase_08 AC 110 ms
74,396 KB
testcase_09 AC 110 ms
74,672 KB
testcase_10 AC 108 ms
74,876 KB
testcase_11 AC 112 ms
74,364 KB
testcase_12 AC 112 ms
74,240 KB
testcase_13 AC 111 ms
74,400 KB
testcase_14 AC 113 ms
74,400 KB
testcase_15 AC 111 ms
74,420 KB
testcase_16 AC 110 ms
74,604 KB
testcase_17 AC 111 ms
74,588 KB
testcase_18 AC 113 ms
74,420 KB
testcase_19 AC 113 ms
74,208 KB
testcase_20 AC 111 ms
74,392 KB
testcase_21 AC 113 ms
74,688 KB
testcase_22 AC 113 ms
74,416 KB
testcase_23 AC 114 ms
74,344 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys,random,bisect
from collections import deque,defaultdict
from heapq import heapify,heappop,heappush
from itertools import permutations
from math import gcd,log

input = lambda :sys.stdin.readline().rstrip()
mi = lambda :map(int,input().split())
li = lambda :list(mi())

class UnionFindVerSize():
    def __init__(self, N):
        self._parent = [n for n in range(0, N)]
        self._size = [1] * N
        self.group = N

    def find_root(self, x):
        if self._parent[x] == x: return x
        self._parent[x] = self.find_root(self._parent[x])
        return self._parent[x]

    def unite(self, x, y):
        gx = self.find_root(x)
        gy = self.find_root(y)
        if gx == gy: return

        self.group -= 1

        if self._size[gx] < self._size[gy]:
            self._parent[gx] = gy
            self._size[gy] += self._size[gx]
        else:
            self._parent[gy] = gx
            self._size[gx] += self._size[gy]

    def get_size(self, x):
        return self._size[self.find_root(x)]

    def is_same_group(self, x, y):
        return self.find_root(x) == self.find_root(y)


N = int(input())
P = [int(p)-1 for p in input().split()]

uf = UnionFindVerSize(N)
for i in range(N):
    uf.unite(i,P[i])

print(N-uf.group)
0