qyliang/CodeNanoFix huggingface dataset:
Buggy code:
s = list(input())
k = int(input())
t = sorted(s)
u = []
v = []
for i in range(min(len(s), 5)):
    u.append(t[i])
for i in range(len(s)):
    if s[i] in u:
        if not [s[i]] in v:
            v.append([s[i]])
        l = [s[i]]
        for j in range(1, 5):
            if i + j == len(s):
                break
            l = l + [s[i + j]]
            if not l in v:
                v.append(l)
for i in range(len(v)):
    v[i] = "".join(v[i])
v.sort()
print(v[k - 1])
print(v)

Fixed code:
s = list(input())
k = int(input())
t = sorted(s)
u = []
v = []
for i in range(min(len(s), 5)):
    u.append(t[i])
for i in range(len(s)):
    if s[i] in u:
        if not [s[i]] in v:
            v.append([s[i]])
        l = [s[i]]
        for j in range(1, 5):
            if i + j == len(s):
                break
            l = l + [s[i + j]]
            if not l in v:
                v.append(l)
for i in range(len(v)):
    v[i] = "".join(v[i])
v.sort()
print(v[k - 1])
Buggy code:
S = input()
K = int(input())
N = len(S)

l = []
for i in range(N):
    for j in range(i+1,min(i+K+1,N)):
        s = S[i:j]
        l.append(s)
l = list(set(l))
l.sort()
print(l[K-1])

Fixed code:
S = input()
K = int(input())
N = len(S)

l = []
for i in range(N):
    for j in range(i+1,min(i+K+1,N+1)):
        s = S[i:j]
        l.append(s)
l = list(set(l))
l.sort()
print(l[K-1])
Buggy code:
s = input()
k = int(input())
l = []
for i in range(1,k):
    for j in range(len(s)-i+1):
        l.append(s[j:j+i])


l = list(set(l))
l.sort()
print(l[k-1])

Fixed code:
s = input()
k = int(input())
l = []
for i in range(1,k+1):
    for j in range(len(s)-i+1):
        l.append(s[j:j+i])


l = list(set(l))
l.sort()
print(l[k-1])
Buggy code:
S = input()
K = int(input())

substring_set = set()
for i in range(len(S)):
    for j in range(min([K, len(S)-i])):
        substring_set.add(S[i, i+j])

print(sorted(list(substring_set))[K-1])


Fixed code:
S = input()
K = int(input())
len_s = len(S)

substring_set = set()
for i in range(len_s):
    for j in range(min([K, len_s-i])):
        substring_set.add(S[i:i+j+1])

sorted_list = sorted(list(substring_set))
print(sorted_list[K-1])
Buggy code:
# K-th Substring
S = input()
# 5000
K = int(input())
# K < 5

s = []
for i in range(len(S)):
    for k in range(K):
        s.append(S[i:i+k])

print(sorted(set(s))[K-1])

Fixed code:
S = input()
# 5000
K = int(input())
# K < 5

s = []
for i in range(len(S)):
    for k in range(1,K+1):
        s.append(S[i:i+k])

print(sorted(set(s))[K-1])
Buggy code:
s=input()
K=int(input())
listA=[]


for j in range(1,K+1):
    for i in range(len(s)):
        listA.append(s[i:i+j])
listA.sort()
x=""
count=0
for i in range(len(listA)):
    if(x!=listA[i]):
        count+=1
        x=listA[i]
    if(count==K):
        print(x)
        break;
print(listA)

Fixed code:
s=input()
K=int(input())
listA=[]


for j in range(1,K+1):
    for i in range(len(s)):
        listA.append(s[i:i+j])
listA.sort()
x=""
count=0
for i in range(len(listA)):
    if(x!=listA[i]):
        count+=1
        x=listA[i]
    if(count==K):
        print(x)
        break;
Buggy code:
s=input()
l=len(s)
k=int(input())
x=set()
for i in range(1,k):
    for j in range(l-i+1):
        x.add(s[j:j+i])
x=sorted(list(x))
print(x[k-1])


Fixed code:
s=input()
l=len(s)
k=int(input())
x=set()
for i in range(1,min(l,k)+1):
    for j in range(l-i+1):
        x.add(s[j:j+i])
x=sorted(list(x))
print(x[k-1])
Buggy code:
s = input()
k = int(input())
a = set()
for i in range(len(s) + 1):
    for j in range(1, 6):
        a.add(s[i:i + j + 1])
print(sorted(list(a))[k - 1])

Fixed code:
s = input()
k = int(input())
a = set()
for i in range(len(s) + 1):
    for j in range(1, 6):
        a.add(s[i:i + j])
print(sorted(list(a))[k])
Buggy code:
s = input()
K = int(input())

l = {s}
for n in range(1, K):
    for i in range(0, len(s)-n+1):
        l.add(s[i:i+n])

l = list(l)

l.sort()

print(l[K-1])

Fixed code:
s = input()
K = int(input())

l = {s}
for n in range(1, K+1):
    for i in range(0, len(s)-n+1):
        l.add(s[i:i+n])

l = list(l)

l.sort()

print(l[K-1])
Buggy code:
# -*- coding: utf-8 -*-

s = input()
K = int(input())

sub = []
for i in range(1,int(len(s))+1):
    for j in range(0,6):
        if s[j:j+i] in sub:
            pass
        else:
            sub.append(s[j:j+i])

sub=sorted(sub)
print(sub[K-1])


Fixed code:
# -*- coding: utf-8 -*-

s = input()
K = int(input())

sub = []
for i in range(1,6):
    for j in range(0,int(len(s))):
        if s[j:j+i] in sub:
            pass
        else:
            sub.append(s[j:j+i])

sub=sorted(sub)
print(sub[K-1])
Buggy code:
s= str(input().strip())
K= int(input().strip())
parts = []
for i in range(len(s)+1):
    for j in range(i+1,len(s)+1):
        flg = 0
        if j-i > K:
            break
        parts.append(s[i:j])
v = sorted(parts)
vv = [v[0]]
tmp = v[0]
for k in range(1,len(v)):
    if v[k]!=tmp:
        vv.append(v[k])
        tmp = v[k]
    if len(vv)==K:
        break
        
print(vv[K])

Fixed code:
s= str(input().strip())
K= int(input().strip())
parts = []
for i in range(len(s)+1):
    for j in range(i+1,len(s)+1):
        flg = 0
        if j-i > K:
            break
        parts.append(s[i:j])
v = sorted(parts)
vv = [v[0]]
tmp = v[0]
for k in range(1,len(v)):
    if v[k]!=tmp:
        vv.append(v[k])
        tmp = v[k]
    if len(vv)==K:
        break
        
print(vv[K-1])
Buggy code:
s=input()
K=int(input())
L=list(s)
S=sorted(set(L))
A=[]

for _ in range(min(3,len(S))):
    H=S[_]
    A.append(H)
    HN=[]
    for i in range(len(L)):
        if L[i]==H:
            HN.append(i)
    for i in HN:
        t=2
        while i+t<len(s)+1 and t<6:
            A.append(''.join(L[i:i+t]))
            t+=1

A=sorted(list(set(A)))
print(A,A[K-1])

Fixed code:
s=input()
K=int(input())
L=list(s)
S=sorted(set(L))
A=[]

for _ in range(min(3,len(S))):
    H=S[_]
    A.append(H)
    HN=[]
    for i in range(len(L)):
        if L[i]==H:
            HN.append(i)
    for i in HN:
        t=2
        while i+t<len(s)+1 and t<6:
            A.append(''.join(L[i:i+t]))
            t+=1

A=sorted(list(set(A)))
print(A[K-1])
Buggy code:
import sys
from functools import reduce
import copy
import math


sys.setrecursionlimit(4100000)


def inputs(num_of_input):
    ins = [input() for i in range(num_of_input)]
    return ins


def solve(inputs):
    S = inputs[0]
    K = int(inputs[1])

    sets = set()
    print(K)
    for i in range(len(S)):
        j = i
        while j < len(S) and j - i <= K:
            j += 1
        sets.add(S[i:j])
    new_sets = set()

    for k in sets:
        for i in range(len(k)):
            j = i
            while j < len(k) and j - i <= K:
                j += 1
                new_sets.add(k[i:j])
    l = list(new_sets)
    l.sort()
    return l[K-1]


def string_to_int(string):
    return list(map(int, string.split()))


if __name__ == "__main__":
    ret = solve(inputs(2))
    print(ret)


Fixed code:
import sys
from functools import reduce
import copy
import math


sys.setrecursionlimit(4100000)


def inputs(num_of_input):
    ins = [input() for i in range(num_of_input)]
    return ins


def solve(inputs):
    S = inputs[0]
    K = int(inputs[1])

    sets = set()
    for i in range(len(S)):
        j = i
        while j < len(S) and j - i <= K:
            j += 1
        sets.add(S[i:j])
    new_sets = set()

    for k in sets:
        for i in range(len(k)):
            j = i
            while j < len(k) and j - i <= K:
                j += 1
                new_sets.add(k[i:j])
    l = list(new_sets)
    l.sort()
    return l[K-1]


def string_to_int(string):
    return list(map(int, string.split()))


if __name__ == "__main__":
    ret = solve(inputs(2))
    print(ret)
Buggy code:
s = input()
K = int(input())

substringList = []
if(len(s) < 5):
    for i in range(len(s)):
        if(i+5 > len(s)):
            for j in range(i, len(s)):
                #print(i,j)
                if(s[i:j+1] in substringList):
                    pass
                else:
                    substringList.append(s[i:j+1])
else:
    for i in range(len(s)):
        if(i+5 > len(s)):
            for j in range(i, i+5):
                #print(i,j)
                if(s[i:j+1] in substringList):
                    pass
                else:
                    substringList.append(s[i:j+1])
#substringList = list(set(substringList))
print(sorted(substringList)[K-1])


Fixed code:
s = input()
K = int(input())

substringList = []
if(len(s) < 50):
    for i in range(len(s)):
            for j in range(i, len(s)):
                #print(i,j)
                if(s[i:j+1] in substringList):
                    pass
                else:
                    substringList.append(s[i:j+1])
else:
    for i in range(len(s)):
            for j in range(i, i+5):
                substringList.append(s[i:j+1])
#substringList = list(set(substringList))
print(sorted(list(set(substringList)))[K-1])
Buggy code:
import sys
s = input()
K = int(input())

a_list = sorted(set(s))
for i in a_list:
  if K == 1:
    print(i)
    sys.exit()
  p = []
  for j, x in enumerate(s):
    if x == i:
      p.append(s[j:min(j+K,len(s))])
  p.sort()
  print(p)
  t = K
  for j in p:
    if len(j) < K:
      t -= len(j) - 1
    else:
      print(j[0:t])
      sys.exit()
  K = t

Fixed code:
import sys
s = input()
K = int(input())

a_list = sorted(set(s))
for i in a_list:
  if K == 1:
    print(i)
    sys.exit()
  p = []
  for j, x in enumerate(s):
    if x == i:
      p.append(s[j:min(j+K,len(s))])
  p.sort()
  t = K
  for j in range(len(p)):
    if j != len(p) - 1:
      if p[j] == p[j+1][0:len(p[j])]:
        continue
    if len(p[j]) < t:
      t -= len(p[j]) - 1
    else:
      print(p[j][0:t])
      sys.exit()
  K = t - 1
Buggy code:
s=input()
k=int(input())
ans=[]
for i in range(len(s)):
  for j in range(len(s)):
    ans.append(s[j:])
  s=list(s)
  s.insert(0,s.pop())
  s=''.join(s)
ans=list(set(ans))
ans.sort()
print(ans[k-1])

Fixed code:
s=input()
k=int(input())
ans=[]
for i in range(len(s)):
  for j in range(max(0,len(s)-5),len(s)):
    ans.append(s[j:])
  s=list(s)
  s.pop()
  s=''.join(s)
ans=list(set(ans))
ans.sort()
print(ans[k-1])
Buggy code:
s = input()
K = int(input())

sub = set()

for i in range(len(s)):
	for j in range(i + 1,i + K + 1):
		sub.add(s[i:j])

ans = list(sorted(sub))

print(sub[K - 1])

Fixed code:
s = input()
K = int(input())

sub = set()

for i in range(len(s)):
	for j in range(i + 1,i + K + 1):
		sub.add(s[i:j])

ans = list(sorted(sub))

print(ans[K - 1])
Buggy code:
s = list(input())
l = int(input())
s1 = sorted(set(s))
ans = set()

for i in range(len(s)):
    sta = s1[i]
    for j in range(len(s)):
        if s[j] == sta:
            ans.add(s[j])
            check = s[j]
            for k in range(j+1,min(len(s),j+5)):
                check = check+s[k]
                ans.add(check)
        if len(ans) >= l:
            ans1 = sorted(ans)
            print(ans1[l-1])
            exit()
ans1 = sorted(ans)
print(ans1[l-1])

Fixed code:
s = list(input())
l = int(input())
s1 = sorted(s)
ans = set()

for i in range(len(s)):
    sta = s1[i]
    for j in range(len(s)):
        if s[j] == sta:
            ans.add(s[j])
            check = s[j]
            for k in range(j+1,min(len(s),j+5)):
                check = check+s[k]
                ans.add(check)
    if len(ans) >= l:
        ans1 = sorted(ans)
        print(ans1[l-1])
        exit()
ans1 = sorted(ans)
print(ans1[l-1])
Buggy code:
if __name__ == '__main__':
    s = input()
    K = int(input())

    subs = []
    for i in range(len(s)):                                                                                                                                                   for j in range(K):
            try:
                subs.append(s[i:i+j+1])
            except:
                break
    subs = set(subs)
    print(sorted(list(subs))[K-1])

Fixed code:
if __name__ == '__main__':
    s = input()
    K = int(input())

    subs = []
    for i in range(len(s)):
      for j in range(K):
            try:
                subs.append(s[i:i+j+1])
            except:
                break
    subs = set(subs)
    print(sorted(list(subs))[K-1])
Buggy code:
s=list(input())

K=int(input())

L=[]

for i in range (1,K+1):
    for j in range (0, len(s)-i+1):
        L.append("".join(s[j:j+1]))

L=sorted(set(L))

print(L[K-1])



Fixed code:
s=list(input())

K=int(input())

L=[]

for i in range (1,K+1):
    for j in range (0, len(s)-i+1):
        L.append("".join(s[j:j+i]))

L=sorted(set(L))

print(L[K-1])
Buggy code:
import sys


def main():
  input = sys.stdin.readline
  S = str(input().strip())
  K = int(input())
  max = min(5, len(S))
  sub = []
  for i in range(len(S)):
    for j in range(1, max+1):
      if i+j > len(S):
        continue
      sub.append(S[i:i+j])
  sub = sorted(sub)
  print(sub[K])


if __name__ == '__main__':
  main()

Fixed code:
import sys


def main():
  input = sys.stdin.readline
  S = str(input().strip())
  K = int(input())
  max = min(5, len(S))
  sub = []
  for i in range(len(S)):
    for j in range(1, max+1):
      if i+j > len(S):
        continue
      sub.append(S[i:i+j])
  sub = sorted(list(set(sub)))
  print(sub[K-1])


if __name__ == '__main__':
  main()
Buggy code:
S = input()
K = int(input())

tmp = set()
for i in range(len(S)):
  for j in range(5):
    tmp.add(S[i:i+j])
print(sorted(tmp)[K])

Fixed code:
S = input()
K = int(input())
tmp = set()
for i in range(len(S)):
  for j in range(5):
    tmp.add(S[i:i+j+1])
print(sorted(tmp)[K-1])
Buggy code:
s = input()
k = int(input())

n = len(s)

d = set()
for i in range(n):
    for j in range(i+1, min(i+k,n+1)):
        tmp = s[i:j]
        d.add(tmp)
a = sorted(list(d))
print(a[k-1])



Fixed code:
s = input()
k = int(input())

n = len(s)

d = set()
for i in range(n):
    for j in range(i+1, min(i+k+1,n+1)):
        tmp = s[i:j]
        d.add(tmp)
a = sorted(list(d))
print(a[k-1])
Buggy code:
def root(i):
  #print(i)
  if par[i] == i:
    return i
  par[i] = root(par[i])
  return par[i]

def union(x, y, n):
  rx = root(x)
  ry = root(y)
  if rx != ry:
    #print(x, y, rx, ry, par)
    par[ry] = rx
    for i in range(n):
      if par[i] == ry:
        par[i] = rx

def same(x, y):
  return par[x] == par[y]

n,m = map(int, input().split(" "))
p = [0] + list(map(int, input().split(" ")))
a = [(list(map(int, input().split(" ")))) for i in range(m)]
par = list(range(n + 1))
count = 0
for i, j in a:
  union(i, j, n)
  #print(par)
#print(p)
for i in range(1, n + 1):
  if same(p[par[i]], par[i]):
    count += 1
print(count)

Fixed code:
def root(i):
  if par[i] == i:
    return i
  par[i] = root(par[i])
  return par[i]

def union(x, y):
  rx = root(x)
  ry = root(y)
  if rx != ry:
    par[ry] = rx

def same(x, y):
  return par[x] == par[y]

n,m = map(int, input().split(" "))
p = [0] + list(map(int, input().split(" ")))
a = [(list(map(int, input().split(" ")))) for i in range(m)]
par = list(range(n + 1))
count = 0
for i, j in a:
  union(i, j)
  #print(par)
#print(p)
for i in range(1, n + 1):
  if same(root(par[p[i]]), root(par[i])):
    count += 1
print(count)
Buggy code:
n,m=map(int,input().split())
p=list(map(int,input().split()))
pa=list(range(n))
def find(u):
  if u!=pa[u]:pa[u]=find(pa[u])
  return pa[u]
for i in range(m):
  a,b=map(int,input().split())
  a=find(a-1)
  b=find(b-1)
  if a!=b:pa[a]=b
c=0
for i in range(n):
  if find(p[i])==find(i):c+=1
print(c)

Fixed code:
n,m=map(int,input().split())
p=list(map(int,input().split()))
pa=list(range(n))
def find(u):
  if u!=pa[u]:pa[u]=find(pa[u])
  return pa[u]
for i in range(m):
  a,b=map(int,input().split())
  a=find(a-1)
  b=find(b-1)
  if a!=b:pa[a]=b
c=0
for i in range(n):
  if find(p[i]-1)==find(i):c+=1
print(c)
Buggy code:
class uft():
    def __init__(self,n):
        self.p=[ -1 for i in range(n)]
        self.r=[ 1 for i in range(n)]
        
    def find(self,x):
        if(self.p[x]==-1):
            return x
        else:
            self.p[x]=self.find(self.p[x])
            return self.find(self.p[x])
        
    def unite(self,x,y):
        x=self.find(x)
        y=self.find(y)
        if(self.r[x]>self.r[y]):
            tmp=x
            x=y
            y=tmp
            
        if(self.r[x]==self.r[y]):
            self.r[y]+=1
            
        if(x!=y):
            self.p[x]=y
        return
    
n,m=map(int,input().split())
p=list(map(int,input().split()))
uf=uft(n)
for i in range(m):
    x,y=map(int,input().split())
    x-=1
    y-=1
    uf.unite(x,y)
ans=0
for i in range(n):
    if(uf.find(i)==uf.find(p[i])):
        ans+=1
        
print(ans)

Fixed code:
class uft():
    def __init__(self,n):
        self.p=[ -1 for i in range(n)]
        self.r=[ 1 for i in range(n)]
        
    def find(self,x):
        if(self.p[x]==-1):
            return x
        else:
            self.p[x]=self.find(self.p[x])
            return self.find(self.p[x])
        
    def unite(self,x,y):
        x=self.find(x)
        y=self.find(y)
        if(self.r[x]>self.r[y]):
            tmp=x
            x=y
            y=tmp
            
        if(self.r[x]==self.r[y]):
            self.r[y]+=1
            
        if(x!=y):
            self.p[x]=y
        return
    
n,m=map(int,input().split())
p=list(map(int,input().split()))
for i in range(n):
  p[i]-=1
uf=uft(n)
for i in range(m):
    x,y=map(int,input().split())
    x-=1
    y-=1
    uf.unite(x,y)
ans=0
for i in range(n):
    if(uf.find(i)==uf.find(p[i])):
        ans+=1
        
print(ans)
Buggy code:
n,m=map(int,input().split())
f=lambda x:int(x)-1
p=list(map(f,input().split()))
par=[j for j in range(n)]
def find(x):
  if x==par[x]:
    return x
  else:
    par[x]=find([par[x]])
    return par[x]

def union(x,y):
  if par[x]!=par[y]:
    par[x]=par[y]

for i in range(m):
  x,y=map(f,input().split())
  union(x,y)

ans=0
for j in range(n):
  if par[j]==par[p[j]]:
    ans+=1
print(ans)

Fixed code:
n,m=map(int,input().split())
p=list(map(int,input().split()))
par=[i for i in range(n)]
 
def find(x):
  if par[x]==x:
    return par[x]
  else:
    par[x]=find(par[x])
    return par[x]
 
def union(x,y):
  fx=find(x)
  fy=find(y)
  if fx!=fy:
    par[fx]=fy
 
f=lambda x:int(x)-1
for mm in range(m):
  x,y=map(f,input().split())
  union(x,y)
 
ans=0
for i in range(n):
  if find(p[i]-1)==find(i):
    ans+=1
print(ans)
Buggy code:
n,m=map(int,input().split())
f=lambda x:int(x)-1
p=list(map(f,input().split()))
par=[j for j in range(n)]
def find(x):
  if x==par[x]:
    return x
  else:
    par[x]=find(par[x])
    return par[x]

def union(a,b):
  ra=find(a)
  rb=find(b)
  if ra!=rb:
    par[ra]=rb

for i in range(m):
  v,w=map(f,input().split())
  union(v,w)

ans=0
for j in range(n):
  if find(j)==find(p[j]):
    ans+=1
print(ans)
print(par)

Fixed code:
n,m=map(int,input().split())
f=lambda x:int(x)-1
p=list(map(f,input().split()))
par=[j for j in range(n)]
def find(x):
  if x==par[x]:
    return x
  else:
    par[x]=find(par[x])
    return par[x]
 
def union(a,b):
  ra=find(a)
  rb=find(b)
  if ra!=rb:
    par[ra]=rb
 
for i in range(m):
  v,w=map(f,input().split())
  union(v,w)
 
ans=0
for j in range(n):
  if find(j)==find(p[j]):
    ans+=1
print(ans)
Buggy code:
N, M = map(int, input().split())
p = list(map(int, input().split()))

class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

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

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

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

    def size(self, x):
        return -self.parents[self.find(x)]

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

unionfind = UnionFind(N)
for _ in range(M):
    x, y = map(int, input().split())
    unionfind.union(x, y)
ans = 0
for i in range(N):
    if i + 1 != p[i]:
        if unionfind.same(p[i], i + 1):
            ans += 1
    else:
        ans += 1
print(ans)

Fixed code:
N, M = map(int, input().split())
p = list(map(int, input().split()))

class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

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

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

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

    def size(self, x):
        return -self.parents[self.find(x)]

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

unionfind = UnionFind(N + 1)
for _ in range(M):
    x, y = map(int, input().split())
    unionfind.union(x, y)
ans = 0
for i in range(N):
    if i + 1 != p[i]:
        if unionfind.same(p[i], i + 1):
            ans += 1
    else:
        ans += 1
print(ans)
Buggy code:
def LI(): return [int(i) for i in input().split()]
def LI_(): return [int(i)-1 for i in input().split()]
def LIR_(n): return [LI_() for i in range(n)]
def D():
    n, m = LI()
    p = LI_()
    xy = LIR_(m)
    size = [1 for _ in range(n)]
    height = [1 for _ in range(n)]
    par = [i for i in range(n)]
    def find(x):
        if par[x] == x:
            return x
        else:
            par[x] = find(par[x])
            return par[x]
    def union(x, y):
        s1 = find(x)
        s2 = find(y)
        if s1 != s2:
            if height[s1] < height[s2]:
                par[s1] = s2
                size[s2] += size[s1]
                size[s1] = 0
            else:
                par[s2] = s1
                size[s1] += size[s2]
                size[s2] = 0
                if height[s1] == height[s2]:
                    height[s1] += 1
 
    for x,y in xy:
        union(x, y)
    ans = 0
    for i in range(n):
        if find(p[i]) == find(i):
            ans += 1
    
    print(ans)

Fixed code:
def LI(): return [int(i) for i in input().split()]
def LI_(): return [int(i)-1 for i in input().split()]
def LIR_(n): return [LI_() for i in range(n)]
def D():
    n, m = LI()
    p = LI_()
    xy = LIR_(m)
    size = [1 for _ in range(n)]
    height = [1 for _ in range(n)]
    par = [i for i in range(n)]
    def find(x):
        if par[x] == x:
            return x
        else:
            par[x] = find(par[x])
            return par[x]
    def union(x, y):
        s1 = find(x)
        s2 = find(y)
        if s1 != s2:
            if height[s1] < height[s2]:
                par[s1] = s2
                size[s2] += size[s1]
                size[s1] = 0
            else:
                par[s2] = s1
                size[s1] += size[s2]
                size[s2] = 0
                if height[s1] == height[s2]:
                    height[s1] += 1
 
    for x,y in xy:
        union(x, y)
    ans = 0
    for i in range(n):
        if find(p[i]) == find(i):
            ans += 1
    
    print(ans)
D()