結果

問題 No.2289 順列ソート
ユーザー 👑 KazunKazun
提出日時 2023-05-05 21:21:18
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 72 ms / 2,000 ms
コード長 3,670 bytes
コンパイル時間 308 ms
コンパイル使用メモリ 87,300 KB
実行使用メモリ 71,564 KB
最終ジャッジ日時 2023-08-15 02:41:25
合計ジャッジ時間 3,113 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 71 ms
71,292 KB
testcase_01 AC 71 ms
71,544 KB
testcase_02 AC 71 ms
71,392 KB
testcase_03 AC 70 ms
71,272 KB
testcase_04 AC 70 ms
71,464 KB
testcase_05 AC 72 ms
71,316 KB
testcase_06 AC 70 ms
71,352 KB
testcase_07 AC 71 ms
71,552 KB
testcase_08 AC 71 ms
71,436 KB
testcase_09 AC 71 ms
71,388 KB
testcase_10 AC 70 ms
71,312 KB
testcase_11 AC 71 ms
71,216 KB
testcase_12 AC 71 ms
71,264 KB
testcase_13 AC 71 ms
71,320 KB
testcase_14 AC 71 ms
71,436 KB
testcase_15 AC 70 ms
71,276 KB
testcase_16 AC 70 ms
71,564 KB
testcase_17 AC 69 ms
71,352 KB
testcase_18 AC 71 ms
71,396 KB
testcase_19 AC 71 ms
71,276 KB
testcase_20 AC 70 ms
71,464 KB
testcase_21 AC 71 ms
71,356 KB
testcase_22 AC 70 ms
71,124 KB
testcase_23 AC 70 ms
71,256 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class Union_Find():
    __slots__=("n","parents","rank","edges","__group_number")
    def __init__(self,N):
        """ 0,1,...,N-1 を要素として初期化する.

        N: 要素数
        """
        self.n=N
        self.parents=[-1]*N
        self.rank=[0]*N
        self.edges=[0]*N
        self.__group_number=N

    def find(self, x):
        """ 要素 x の属している族を調べる.

        x: 要素
        """

        a=x
        while self.parents[a]>=0:
            a=self.parents[a]

        while self.parents[x]>=0:
            y=self.parents[x]
            self.parents[x]=a
            x=y

        return a

    def union(self, x, y):
        """ 要素 x,y を同一視する.

        [input]
        x,y: 要素

        [output]
        元々が非連結 → True
        元々が連結 → False
        """
        x=self.find(x)
        y=self.find(y)

        if x==y:
            self.edges[x]+=1
            return False

        if self.rank[x]<self.rank[y]:
            x,y=y,x

        self.__group_number-=1

        self.edges[x]+=self.edges[y]+1
        self.edges[y]=0

        self.parents[x]+=self.parents[y]
        self.parents[y]=x

        if self.rank[x]==self.rank[y]:
            self.rank[x]+=1
        return True

    def size(self, x):
        """ 要素 x の属している族の要素の数.

        x: 要素
        """
        return -self.parents[self.find(x)]

    def same(self, x, y):
        """ 要素 x,y は同一視されているか?

        x,y: 要素
        """
        return self.find(x) == self.find(y)

    def members(self, x):
        """ 要素 x が属している族の要素.
        ※族の要素の個数が欲しいときは size を使うこと!!

        x: 要素
        """
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def edge_count(self, x):
        """ 要素 x が属する族の辺の本数を求める.

        x: 要素
        """
        return self.edges[self.find(x)]

    def is_tree(self, x):
        """ 要素 x が属する族が木かどうかを判定する.

        x: 要素
        """
        return self.size(x)==self.edges[self.find(x)]+1

    def tree_count(self):
        """ 木になっている属の個数を求める.
        """

        return sum(self.is_tree(g) for g in self.representative())

    def representative(self):
        """ 代表元のリスト
        """
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        """ 族の個数
        """

        return self.__group_number

    def all_group_members(self):
        """ 全ての族の出力
        """
        X={r:[] for r in self.representative()}
        for k in range(self.n):
            X[self.find(k)].append(k)
        return X

    def group_list(self):
        """ 各要素が属している族のリストを出力する.

        """
        return [self.find(x) for x in range(self.n)]

    def refresh(self):
        for i in range(self.n):
            _=self.find(i)

    def __str__(self):
        return str(self.all_group_members().values())[13:-2]

    def __repr__(self):
        return "Union Find : "+str(self)

    def __getitem__(self,index):
        return self.find(index)

    def __setitem__(self,x,y):
        self.union(x,y)

#==================================================
def solve():
    N=int(input())
    P=[0]+list(map(int,input().split()))

    U=Union_Find(N+1)
    for i in range(N+1):
        U.union(i,P[i])
    return (N+1)-U.group_count()

#==================================================
print(solve())
0