Problem p03354 (python)

Fixed submission:
n,m = map(int, input().split())
p = list(map(int, input().split()))
par = [i for i in range(n)]
rank = [0 for x in range(n)]

def root(x):
	tmp = par[x]
	if tmp == x:
		return x
	else:
		a = root(tmp)
		par[x] = a
		return a

def merge(x, y):
	x = root(x)
	y = root(y)
	if x == y:
		return
	else:
		if rank[x] > rank[y]:
			par[y] = x
			rank[y] += rank[x]
		else:
			par[x] = y
			rank[x] += rank[y]
		return

for i in range(m):
	x, y = map(int, input().split())
	merge(x-1, y-1)

ans = 0

for i in range(n):
	if p[i]-1 == i:
		ans += 1
	elif root(p[i]-1) == root(i):
		ans += 1

print(ans)

Buggy submission (verdict: Runtime Error):
n,m = map(int, input().split())
p = list(map(int, input().split()))
par = [i for i in range[n]]
rank = [0 for x in range(n)]

def root(x):
	tmp = par[x]
	if tmp == x:
		return x
	else:
		a = root[tmp]
		par[x] = a
		return a

def merge(x, y):
	x = root(x)
	y = root(y)
	if x == y:
		return
	else:
		if rank[x] > rank[y]:
			par[y] = x
			rank[y] += rank[x]
		else:
			par[x] = y
			rank[x] += rank[y]
	return

for i in range(m):
	x, y = map(int, input().split())
	merge(x-1, y-1)

ans = 0

for i in range(n):
	if p[i]-1 == i:
		ans += 1
	elif root(p[i]-1) == root(i):
		ans += 1

print(ans)

Buggy submission (verdict: Runtime Error):
n,m = map(int, input().split())
p = list(map(int, input().split()))
par = list(range(n))
rank = [0 for x in range(n)]

def root(x):
	if par[x] == x:
		return x
	else:
		return root(par[x])
def merge(x, y):
	x = root(x)
	y = root(y)
	if x == y:
		pass
	else:
		if rank[x] > rank[y]:
			par[y] = x
			rank[y] += rank[x]
		else:
			par[x] = y
			rank[x] += rank[y]
	pass

for i in range(m):
	x, y = map(int, input().split())
	merge(x-1, y-1)

ans = 0

for i in range(n):
	if p[i]-1 == i:
		ans += 1
	elif root(p[i]-1) == root(i):
		ans += 1

print(ans)

Buggy submission (verdict: Runtime Error):
n,m = map(int, input().split())
p = list(map(int, input().split()))
par = list(range(n))
rank = [0 for x in range(n)]

def root(x):
	if par[x] == x:
		return x
	else:
		return root(par[x])
def merge(x, y):
	x = root(x)
	y = root(y)
	if x == y:
		pass
	else:
		if rank[x] > rank[y]:
			par[y] = x
			rank[y] += 1
		else:
			par[x] = y
			rank[x] += 1
	pass

for i in range(m):
	x, y = map(int, input().split())
	merge(x-1, y-1)

ans = 0

for i in range(n):
	if p[i]-1 == i:
		ans += 1
	elif root(p[i]-1) == root(i):
		ans += 1

print(ans)

Buggy submission (verdict: Runtime Error):
n,m = map(int, input().split())
p = list(map(int, input().split()))
par = list(range(n))
rank = [0 for x in range(n)]

def root(x):
	if par[x] == x:
		return x
	else:
		return root(par[x])
def merge(x, y):
	x = root(x)
	y = root(y)
	if x == y:
		pass
	else:
		if rank[x] > rank[y]:
			par[y] = x
			rank[y] += 1
		else:
			par[x] = y
			rank[x] += 1
	pass

for i in range(m):
	x, y = map(int, input().split())
	merge(x-1, y-1)

ans = 0

for i in range(n):
	if root(p[i]-1) == root(i):
		ans += 1

print(ans)

Buggy submission (verdict: Runtime Error):
import numpy as np

n,m = map(int, input().split())
p = list(map(int, input().split()))
par = np.arange(n)
rank = np.zeros(n)

def root(x):
	if par[x] == x:
		return x
	else:
		return root(par[x])
def merge(x, y):
	x = root(x)
	y = root(y)
	if x == y:
		pass
	else:
		if rank[x] > rank[y]:
			par[y] = x
			rank[y] += 1
		else:
			par[x] = y
			rank[x] += 1
	pass

for i in range(m):
	x, y = map(int, input().split())
	merge(x-1, y-1)

ans = 0

for i in range(n):
	if root(p[i]-1) == root(i):
		ans += 1

print(ans)

Buggy submission (verdict: Runtime Error):
import numpy as np

n,m = map(int, input().split())
p = np.array(list(map(int, input().split())))
p = p-1
q = np.zeros((m,2), dtype='uint32')
for i in range(m):
	q[i] = list(map(int, input().split()))
q=q-1
par = np.arange(n)
rank = np.zeros(n)

def root(x):
	if par[x] == x:
		return x
	else:
		return root(par[x])
def merge(x, y):
	x = root(x)
	y = root(y)
	if x == y:
		pass
	else:
		if rank[x] > rank[y]:
			par[y] = x
			rank[y] += 1
		else:
			par[x] = y
			rank[x] += 1
	pass

for x,y in q:
	merge(x,y)

ans = 0

for i in range(n):
	if root(p[i]) == root(i):
		ans += 1

print(ans)

Buggy submission (verdict: Runtime Error):
import numpy as np

n,m = map(int, input().split())
p = np.array(list(map(int, input().split())))
p = p-1
q = np.zeros((m,2), dtype='uint32')
for i in range(m):
	q[i] = list(map(int, input().split()))
q=q-1
par = np.arange(n)

def root(x):
	if par[x] == x:
		return x
	else:
		return root(par[x])
def merge(x, y):
	x = root(x)
	y = root(y)
	if x != y:
		par[x] = y
	pass

for x,y in q:
	merge(x,y)

tree = dict()
tree2 = dict()

for i in range(n):
	tree.setdefault(root(i), set())
	tree2.setdefault(root(i), set())
	tree[root(i)].add(i) 
	tree2[root(i)].add(p[i])

ans = 0

for value, value2 in zip(tree.values(), tree2.values()):
	ans += len(value & value2)

print(ans)

Buggy submission (verdict: Runtime Error):
import numpy as np

n,m = map(int, input().split())
p = np.array(list(map(int, input().split())))
p = p-1
q = np.zeros((m,2), dtype='uint32')
for i in range(m):
	q[i] = list(map(int, input().split()))
q=q-1
par = np.arange(n)

def root(x):
	if par[x] == x:
		return x
	else:
		return root(par[x])
def merge(x, y):
	x = root(x)
	y = root(y)
	if x != y:
		par[x] = y
	pass

for x,y in q:
	merge(x,y)

tree = dict()
tree2 = dict()

for i in range(n):
	tree.setdefault(root(i), set())
	tree2.setdefault(root(i), set())
	tree[root(i)].add(i) 
	tree2[root(i)].add(p[i])

ans = 0

for value, value2 in zip(tree.values(), tree2.values()):
	ans += len(value & value2)

print(ans)

Buggy submission (verdict: Runtime Error):
import numpy as np

n,m = map(int, input().split())
p = np.array(list(map(int, input().split())))
p = p-1
q = np.zeros((m,2), dtype='uint32')
for i in range(m):
	q[i] = list(map(int, input().split()))
q=q-1
par = np.arange(n)

def root(x):
	if par[x] == x:
		return x
	else:
		return root(par[x])
def merge(x, y):
	x = root(x)
	y = root(y)
	if x != y:
		par[x] = y
	pass

for x,y in q:
	merge(x,y)

tree = dict()
tree2 = dict()

for i in range(n):
	tree.setdefault(root(i), set())
	tree2.setdefault(root(i), set())
	tree[root(i)].add(i) 
	tree2[root(i)].add(p[i])

ans = set()

for value, value2 in zip(tree.values(), tree2.values()):
	ans = ans | value & value2

print(len(ans))

Buggy submission (verdict: Runtime Error):
import numpy as np

n,m = map(int, input().split())
p = np.array(list(map(int, input().split())))
p = p-1
q = np.zeros((m,2), dtype='uint32')
for i in range(m):
	q[i] = list(map(int, input().split()))
q=q-1
print(p)
print(q)
par = np.arange(n)
print(par)

def root(x):
	if par[x] == x:
		return x
	else:
		return root(par[x])
def merge(x, y):
	x = root(x)
	y = root(y)
	if x != y:
		par[x] = y
	pass

for x,y in q:
	merge(x,y)

tree = dict()
tree2 = dict()

for i in range(n):
	tree.setdefault(root(i), set())
	tree2.setdefault(root(i), set())
	tree[root(i)].add(i) 
	tree2[root(i)].add(p[i])

print(par)

ans = set()

for value in tree.items():
	print(value)

for item in tree2.items():
	print(item)


for value, value2 in zip(tree.values(), tree2.values()):
	ans = ans | value & value2

print(len(ans))