結果
| 問題 | No.3650 Teleportation Cycles |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2026-08-28 21:52:22 |
| 言語 | PyPy3 (7.3.17) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 984 bytes |
| 記録 | |
| コンパイル時間 | 688 ms |
| コンパイル使用メモリ | 95,852 KB |
| 実行使用メモリ | 145,664 KB |
| 最終ジャッジ日時 | 2026-08-28 21:52:30 |
| 合計ジャッジ時間 | 6,053 ms |
|
ジャッジサーバーID (参考情報) |
judge1_0 / judge2_0 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 11 WA * 25 |
ソースコード
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):
t = a[i]-1
if t in visit:
continue
while not t in visit:
visit.add(t)
t = a[t]-1
res += 1
print(res)