結果

問題 No.3650 Teleportation Cycles
コンテスト
ユーザー Alice Papilio
提出日時 2026-08-28 21:37:48
言語 PyPy3
(7.3.17)
コンパイル:
pypy3 -mpy_compile _filename_
実行:
pypy3 _filename_
結果
WA  
実行時間 -
コード長 979 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 259 ms
コンパイル使用メモリ 95,980 KB
実行使用メモリ 145,792 KB
最終ジャッジ日時 2026-08-28 21:38:40
合計ジャッジ時間 5,595 ms
ジャッジサーバーID
(参考情報)
judge2_0 / judge1_1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 9 WA * 27
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

class Unionfind:
    def __init__(self, size):
        self.root = [x for x in range(size + 1)]
        self.rank = [1] * (size + 1)

    def find(self, x):
        if x != self.root[x]:
            self.root[x] = self.find(self.root[x])

        return self.root[x]

    def union(self, x, y):
        rootx = self.find(x)
        rooty = self.find(y)

        if rootx != rooty:
            if self.rank[rootx] > self.rank[rooty]:
                self.root[rooty] = rootx
            elif self.rank[rootx] < self.rank[rooty]:
                self.root[rootx] = rooty
            else:
                self.root[rooty] = rootx
                self.rank[rootx] += 1

    def connected(self, x, y):
        return self.find(x) == self.find(y)


n = int(input())
a = list(map(int, input().split()))
res = 0
visit = set()

for i in range(n):
    if i in visit:
        continue

    t = i
    while not t in visit:
        visit.add(t)
        t = a[t]-1

    res += 1


print(res)
0