TnT/Multi_CodeNet4Repair huggingface dataset:
Problem: You are given a string s. Among the **different** substrings of s, print the K-th lexicographically smallest one.  A substring of s is a string obtained by taking out a non-empty contiguous part in s. For example, if s = `ababc`, `a`, `bab` and `ababc` are substrings of s, while `ac`, `z` and an empty string are not. Also, we say that substrings are different when they are different as strings.  Let X = x_{1}x_{2}...x_{n} and Y = y_{1}y_{2}...y_{m} be two distinct strings. X is lexicographically larger than Y if and only if Y is a prefix of X or x_{j} > y_{j} where j is the smallest integer such that x_{j} \neq y_{j}.

Submission attempts (in order):

s = input()
k = int(input())
ans = []
for i in range(len(s)):
    for j in range(i + 1, min(len(s), i + 5 + 1)):
        t = "".join(s[i:j])
        if t not in ans:
            ans.append("".join(s[i:j]))
            ans = sorted(ans)[:5]
print(ans[k - 1])


,
s = input() + " "
k = int(input())
ans = []
for i in range(len(s)):
    for j in range(i + 1, min(len(s), i + 5 + 1)):
        t = "".join(s[i:j])
        if t not in ans:
            ans.append(t)
            ans = sorted(ans)[:6]
print(ans[k - 1])


s = input()
k = int(input())
lis = []

for i in range(len(s)):
    for j in range(k):
        lis.append("".join(s[i:i + j + 1]))
lis = sorted(list(set(lis)))

print(lis[k - 1])


Verdicts: Runtime Error

Runtime Error

Accepted
Problem: You are given a string s. Among the **different** substrings of s, print the K-th lexicographically smallest one.  A substring of s is a string obtained by taking out a non-empty contiguous part in s. For example, if s = `ababc`, `a`, `bab` and `ababc` are substrings of s, while `ac`, `z` and an empty string are not. Also, we say that substrings are different when they are different as strings.  Let X = x_{1}x_{2}...x_{n} and Y = y_{1}y_{2}...y_{m} be two distinct strings. X is lexicographically larger than Y if and only if Y is a prefix of X or x_{j} > y_{j} where j is the smallest integer such that x_{j} \neq y_{j}.

Submission attempts (in order):
# n = int(input())
# a, b = map(int, input().split())
# c = list(map(int, input().split()))
# d = [list(map(int, input().split())) for _ in range(n)]

alphabet = "abcdefghijklnmopqrstuvwxyz"
s = input()
k = int(input())
s_set = set(s)
ans = set()

if k == 1:
    s_set_list = list(s_set)
    s_set_list.sort()
    print(s_set_list[0])

elif k >= 2:
    for c in alphabet:
        for i in range(len(s)):
            if c == s[i]:
                ans.add(c)
                if i == len(s[i]):
                    break
                for j in range(i+1, i+k+1):
                    if j > len(s):
                        break
                    ans.add(s[i:j])
        if len(ans) >= k:
            break


    ans_list = list(ans)
    ans_list.sort()
    print(ans_list)
    print(ans_list[k - 1])

# n = int(input())
# a, b = map(int, input().split())
# c = list(map(int, input().split()))
# d = [list(map(int, input().split())) for _ in range(n)]

alphabet = "abcdefghijklnmopqrstuvwxyz"
s = input()
k = int(input())
s_set = set(s)
ans = set()

if k == 1:
    s_set_list = list(s_set)
    s_set_list.sort()
    print(s_set_list[0])

elif k >= 2:
    for c in alphabet:
        for i in range(len(s)):
            if c == s[i]:
                ans.add(c)
                if i == len(s):
                    break
                for j in range(i+1, i+k+2):
                    if j > len(s):
                        break
                    ans.add(s[i:j])
        if len(ans) > k:
            break


    ans_list = list(ans)
    ans_list.sort()
    print(ans_list[k - 1])

Verdicts: Wrong Answer

Accepted
Problem: You are given a string s. Among the **different** substrings of s, print the K-th lexicographically smallest one.  A substring of s is a string obtained by taking out a non-empty contiguous part in s. For example, if s = `ababc`, `a`, `bab` and `ababc` are substrings of s, while `ac`, `z` and an empty string are not. Also, we say that substrings are different when they are different as strings.  Let X = x_{1}x_{2}...x_{n} and Y = y_{1}y_{2}...y_{m} be two distinct strings. X is lexicographically larger than Y if and only if Y is a prefix of X or x_{j} > y_{j} where j is the smallest integer such that x_{j} \neq y_{j}.

Submission attempts (in order):
s = list(input())
k = int(input())
sub = []
sub += s[:]
 
for j in range(2,K+1):
    charGram = [''.join(s[i:i+j]) for i in range(len(s)-1)]
    sub += charGram
sub = list(set(sub))
print(sorted(sub)[k-1])

s = list(input())
k = int(input())
sub = []
sub += s[:]
 
for j in range(2,k+1):
    charGram = [''.join(s[i:i+j]) for i in range(len(s)-1)]
    sub += charGram
sub = list(set(sub))
print(sorted(sub)[k-1])

Verdicts: Runtime Error

Accepted
Problem: You are given a string s. Among the **different** substrings of s, print the K-th lexicographically smallest one.  A substring of s is a string obtained by taking out a non-empty contiguous part in s. For example, if s = `ababc`, `a`, `bab` and `ababc` are substrings of s, while `ac`, `z` and an empty string are not. Also, we say that substrings are different when they are different as strings.  Let X = x_{1}x_{2}...x_{n} and Y = y_{1}y_{2}...y_{m} be two distinct strings. X is lexicographically larger than Y if and only if Y is a prefix of X or x_{j} > y_{j} where j is the smallest integer such that x_{j} \neq y_{j}.

Submission attempts (in order):
s = str(input())
n = int(input())

cand = [s[0]]
for i in range(len(s)):
    if s[i] > cand[len(cand)-1]:
        continue
    for j in range(i, len(s)):
        tmp = s[i:j+1]
        #print(i,j, tmp, cand)
        if tmp in cand:
            continue
        cand += [tmp]
        if len(cand) > n:
            cand = sorted(cand)
            cand.pop()


print(cand.pop())


s = str(input())
n = int(input())

for i in range(len(s)):
    print(i, s[i])

cand = [str(s[0])]
for i in range(len(s)):
    if len(cand) == n and s[i] > cand[-1]:
        continue
    for j in range(i, len(s)):
        tmp = str(s[i:j+1])
        #print(i,j, tmp, cand)
        if tmp in cand:
            continue
        cand += [tmp]
        cand = sorted(cand)
        if len(cand) > n:
            cand.pop()


print(cand.pop())


s = str(input())
n = int(input())

cand = [str(s[0])]
for i in range(len(s)):
    if s[i] > cand[-1]:
        continue
    for j in range(i, len(s)):
        tmp = s[i:j+1]
        #print(i,j, tmp, cand)
        if tmp in cand:
            continue
        cand += [tmp]
        #cand = sorted(cand)
        cand.sort()
        if len(cand) > n:
            cand.pop()


print(cand.pop())


s = input()
k = int(input())

substr = []
for n in range(1, k+1):
    for i in range(len(s)-n+1):
        substr.append(s[i:i+n])

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


Verdicts: Wrong Answer

Wrong Answer

Wrong Answer

Accepted
Problem: You are given a string s. Among the **different** substrings of s, print the K-th lexicographically smallest one.  A substring of s is a string obtained by taking out a non-empty contiguous part in s. For example, if s = `ababc`, `a`, `bab` and `ababc` are substrings of s, while `ac`, `z` and an empty string are not. Also, we say that substrings are different when they are different as strings.  Let X = x_{1}x_{2}...x_{n} and Y = y_{1}y_{2}...y_{m} be two distinct strings. X is lexicographically larger than Y if and only if Y is a prefix of X or x_{j} > y_{j} where j is the smallest integer such that x_{j} \neq y_{j}.

Submission attempts (in order):
s = input()
k = int(input())
sub = set()
for i in range(n):
  for j in range(1,k):
    sub.add(s[i:i+j])
print(sorted(list(sub))[k-1])

n,m = map(int, input().split())
p = list(map(int,input().split()))
d = [list() for _ in range(n+1)]
cnt = 0
for _ in range(m):
  x,y = map(int, input().split())
  d[x].append(y)
  d[y].append(x)
gr = [0]*(n+1)
gr[0] = -1
gn = 0
for x in range(1,n+1):
  if gr[x] == 0:
    gn += 1
    gr[x] = gn
    l = [x]
    while l:
      cur = l.pop()
      for i in d[cur]:
        if i not in l and gr[i] == 0:
          gr[i] = gn
          l.append(i)
for i in range(n):
  if gr[i+1] == gr[p[i]]:
    cnt += 1
print(cnt)

s = input()
k = int(input())
sub = set()
for i in range(len(s)):
  for j in range(1,k+1):
    sub.add(s[i:i+j])
print(sorted(list(sub))[k-1])

Verdicts: Runtime Error

Runtime Error

Accepted
Problem: You are given a string s. Among the **different** substrings of s, print the K-th lexicographically smallest one.  A substring of s is a string obtained by taking out a non-empty contiguous part in s. For example, if s = `ababc`, `a`, `bab` and `ababc` are substrings of s, while `ac`, `z` and an empty string are not. Also, we say that substrings are different when they are different as strings.  Let X = x_{1}x_{2}...x_{n} and Y = y_{1}y_{2}...y_{m} be two distinct strings. X is lexicographically larger than Y if and only if Y is a prefix of X or x_{j} > y_{j} where j is the smallest integer such that x_{j} \neq y_{j}.

Submission attempts (in order):
s=input()
n=int(input())
lst=[]
for i in range(len(s)):
  for j in range(i+1,min(i+n+1,len(s)+1)):
    lst.append(s[i:j])
sub=set(lst)
sub.sort()
print(sub[n-1])

s=input()
n=int(input())
lst=[]
for i in range(len(s)):
  for j in range(i+1,min(i+n+1,len(s)+1)):
    lst.append(s[i:j])
sub=set(lst)
sub=sorted(sub)
print(sub[n-1])


Verdicts: Runtime Error

Accepted
Problem: You are given a string s. Among the **different** substrings of s, print the K-th lexicographically smallest one.  A substring of s is a string obtained by taking out a non-empty contiguous part in s. For example, if s = `ababc`, `a`, `bab` and `ababc` are substrings of s, while `ac`, `z` and an empty string are not. Also, we say that substrings are different when they are different as strings.  Let X = x_{1}x_{2}...x_{n} and Y = y_{1}y_{2}...y_{m} be two distinct strings. X is lexicographically larger than Y if and only if Y is a prefix of X or x_{j} > y_{j} where j is the smallest integer such that x_{j} \neq y_{j}.

Submission attempts (in order):
import bisect, collections, copy, heapq, itertools, math, string, sys
input = lambda: sys.stdin.readline().rstrip() 
sys.setrecursionlimit(10**7)
INF = float('inf')
def I(): return int(input())
def F(): return float(input())
def SS(): return input()
def LI(): return [int(x) for x in input().split()]
def LI_(): return [int(x)-1 for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LSS(): return input().split()

def resolve():
    N, A, B, C, D = LI()
    A -= 1
    B -= 1
    C -= 1
    D -= 1
    S = SS()

    
    if '##' in S[A:C+1] or '##' in S[B:D+1]:
        print('No')
    else:
        
        if C < D:
            print('Yes')
        
        else:
            
            if '...' in S[B:D+1]:
                print('Yes')
            else:
                print('No')

if __name__ == '__main__':
    resolve()


import bisect, collections, copy, heapq, itertools, math, string, sys
input = lambda: sys.stdin.readline().rstrip() 
sys.setrecursionlimit(10**7)
INF = float('inf')
def I(): return int(input())
def F(): return float(input())
def SS(): return input()
def LI(): return [int(x) for x in input().split()]
def LI_(): return [int(x)-1 for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LSS(): return input().split()

def resolve():
    s = SS()
    K = I()

    l = [[] for _ in range(26)]
    for i in range(len(s)):
        l[ord(s[i])-ord('a')].append(i)

    for i in l:
        tmp = set()
        for j in i:
            for k in range(5):
                if j + k <= len(s) - 1:
                    tmp.add(s[j:j+k+1])
        tmp = list(tmp)
        tmp.sort()
        if len(tmp) >= K:
            print(tmp[K-1])
            break
        else:
            K -= len(tmp)

if __name__ == '__main__':
    resolve()


Verdicts: Runtime Error

Accepted
Problem: You are given a string s. Among the **different** substrings of s, print the K-th lexicographically smallest one.  A substring of s is a string obtained by taking out a non-empty contiguous part in s. For example, if s = `ababc`, `a`, `bab` and `ababc` are substrings of s, while `ac`, `z` and an empty string are not. Also, we say that substrings are different when they are different as strings.  Let X = x_{1}x_{2}...x_{n} and Y = y_{1}y_{2}...y_{m} be two distinct strings. X is lexicographically larger than Y if and only if Y is a prefix of X or x_{j} > y_{j} where j is the smallest integer such that x_{j} \neq y_{j}.

Submission attempts (in order):
S = str(input())
K = int(input())

sub = set()
for i in range(1,min(K, len(S))+1):
    for j in range(len(S)-K+1):
        sub.add(S[j:j+i])
m=list(sub)
m.sort()
print(m[K-1])

S = str(input())
K = int(input())
 
sub = set()
for i in range(1,min(K, len(S))+1):
    for j in range(len(S)-i+1):
        sub.add(S[j:j+i])
        print(S[j:j+i])
m=list(sub)
m.sort()
print(m)
print(m[K-1])

S = str(input())
K = int(input())
 
sub = set()
for i in range(1,min(K, len(S))+1):
    for j in range(len(S)-i+1):
        sub.add(S[j:j+i])
m=list(sub)
m.sort()
print(m[K-1])

Verdicts: Runtime Error

Wrong Answer

Accepted
Problem: You are given a string s. Among the **different** substrings of s, print the K-th lexicographically smallest one.  A substring of s is a string obtained by taking out a non-empty contiguous part in s. For example, if s = `ababc`, `a`, `bab` and `ababc` are substrings of s, while `ac`, `z` and an empty string are not. Also, we say that substrings are different when they are different as strings.  Let X = x_{1}x_{2}...x_{n} and Y = y_{1}y_{2}...y_{m} be two distinct strings. X is lexicographically larger than Y if and only if Y is a prefix of X or x_{j} > y_{j} where j is the smallest integer such that x_{j} \neq y_{j}.

Submission attempts (in order):
import heapq

S = input()
K = int(input())

q = []

for i in range(len(S)):
    for j in range(i, len(S) + 1):
        heapq.heappush(q, S[i:j])

last = ''
print(q)
while K > 0:
    s = heapq.heappop(q)
    if s != last:
        K -= 1
        last = s

print(last)


import heapq

S = input()
K = int(input())

q = []

for i in range(len(S)):
    for j in range(i, min(len(S), i + K) + 1):
        heapq.heappush(q, S[i:j])

last = ''
while K > 0:
    s = heapq.heappop(q)
    if s != last:
        K -= 1
        last = s

print(last)


Verdicts: Wrong Answer

Accepted
Problem: You are given a string s. Among the **different** substrings of s, print the K-th lexicographically smallest one.  A substring of s is a string obtained by taking out a non-empty contiguous part in s. For example, if s = `ababc`, `a`, `bab` and `ababc` are substrings of s, while `ac`, `z` and an empty string are not. Also, we say that substrings are different when they are different as strings.  Let X = x_{1}x_{2}...x_{n} and Y = y_{1}y_{2}...y_{m} be two distinct strings. X is lexicographically larger than Y if and only if Y is a prefix of X or x_{j} > y_{j} where j is the smallest integer such that x_{j} \neq y_{j}.

Submission attempts (in order):
s = str(input())
k = str(input())
lis = []
ans = "zzz"
li = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]
for k in range(6):
	for i in range(len(s)):
		try:
			if s[i:i+k] == "":
				continue
			if s[i:i+k] not in lis:
				lis.append(s[i:i+k])
		except:
			continue
lis.sort()

for i in range(k-1):
	lis.remove(lis[0])

print(lis[0])

s = str(input())
k = str(input())
lis = []
for u in range(6):
	for i in range(len(s)):
		try:
			if s[i:i+u] == "":
				continue
			if s[i:i+u] not in lis:
				lis.append(s[i:i+u])
		except:
			continue
lis.sort()
print(lis[k-1])

s = str(input())
k = int(input())
lis = []
for u in range(6):
	for i in range(len(s)):
		try:
			if s[i:i+u] == "":
				continue
			if s[i:i+u] not in lis:
				lis.append(s[i:i+u])
		except:
			continue
lis.sort()
print(lis[k-1])


Verdicts: Runtime Error

Runtime Error

Accepted
Problem: You are given a string s. Among the **different** substrings of s, print the K-th lexicographically smallest one.  A substring of s is a string obtained by taking out a non-empty contiguous part in s. For example, if s = `ababc`, `a`, `bab` and `ababc` are substrings of s, while `ac`, `z` and an empty string are not. Also, we say that substrings are different when they are different as strings.  Let X = x_{1}x_{2}...x_{n} and Y = y_{1}y_{2}...y_{m} be two distinct strings. X is lexicographically larger than Y if and only if Y is a prefix of X or x_{j} > y_{j} where j is the smallest integer such that x_{j} \neq y_{j}.

Submission attempts (in order):
s=input()
K=int(input())
l=len(s)
d=[[s[i],l-i] for i in range(len(s))]
d=sorted(d)
h=dict()
ans=0
ca=d[0][0]   

for i in d:
    if(i[0]!=i[0][0]):
        break
    z=min(i[1]+1,6)
    for j in range(1,z):
        if(s[l-i[1]:l-i[1]+j] not in h):
            ans+=1
            h.update({s[l-i[1]:l-i[1]+j]:1})
        else:
            pass


h=sorted(h)

s=input()
K=int(input())
l=len(s)
d=[[s[i],l-i] for i in range(len(s))]
d=sorted(d)
h=dict()
ans=0
ca=d[0][0]   

for i in d:
    if(i[0]!=i[0][0]):
        break
    z=min(i[1]+1,6)
    for j in range(1,z):
        if(s[l-i[1]:l-i[1]+j] not in h):
            ans+=1
            h.update({s[l-i[1]:l-i[1]+j]:1})
        else:
            pass


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

Verdicts: Wrong Answer

Accepted
Problem: You are given a string s. Among the **different** substrings of s, print the K-th lexicographically smallest one.  A substring of s is a string obtained by taking out a non-empty contiguous part in s. For example, if s = `ababc`, `a`, `bab` and `ababc` are substrings of s, while `ac`, `z` and an empty string are not. Also, we say that substrings are different when they are different as strings.  Let X = x_{1}x_{2}...x_{n} and Y = y_{1}y_{2}...y_{m} be two distinct strings. X is lexicographically larger than Y if and only if Y is a prefix of X or x_{j} > y_{j} where j is the smallest integer such that x_{j} \neq y_{j}.

Submission attempts (in order):
s = str(input())
k = int(input())
sub = []
c = 5
if len(s)  < 5:
  c = len(s)
for i in range(c):
  for j in range(len(s)-i-1):
    sub.append(s[j:j+i+1])

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


s = str(input())
k = int(input())
sub = []
c = 5
if len(s)  < 5:
  c = len(s)
for i in range(c):
  for j in range(len(s)-i):
    sub.append(s[j:j+i+1])

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


Verdicts: Runtime Error

Accepted
Problem: You are given a string s. Among the **different** substrings of s, print the K-th lexicographically smallest one.  A substring of s is a string obtained by taking out a non-empty contiguous part in s. For example, if s = `ababc`, `a`, `bab` and `ababc` are substrings of s, while `ac`, `z` and an empty string are not. Also, we say that substrings are different when they are different as strings.  Let X = x_{1}x_{2}...x_{n} and Y = y_{1}y_{2}...y_{m} be two distinct strings. X is lexicographically larger than Y if and only if Y is a prefix of X or x_{j} > y_{j} where j is the smallest integer such that x_{j} \neq y_{j}.

Submission attempts (in order):
s = input().strip()
k = int(input())

ss = set()
for i in range(len(s)):
    for j in range(i, len(s)):
        ss.add(s[i:j+1])

print('test')

def all_index(s, c):
    ins = []
    for i in range(len(s)):
        if s[i] == c:
            ins.append(i)

    return ins


s = input().strip()
k = int(input())

ss = set()
alphabets = sorted(list(set(s)))
for c in alphabets:
    ins = all_index(s, c)
    for i in ins:
        for j in range(i, len(s)):
            if (j+1 - i) > k:
                break

            ss.add(s[i:j+1])

    if len(ss) >= k:
        break

print(sorted(list(ss))[k-1])

Verdicts: Wrong Answer

Accepted
Problem: You are given a string s. Among the **different** substrings of s, print the K-th lexicographically smallest one.  A substring of s is a string obtained by taking out a non-empty contiguous part in s. For example, if s = `ababc`, `a`, `bab` and `ababc` are substrings of s, while `ac`, `z` and an empty string are not. Also, we say that substrings are different when they are different as strings.  Let X = x_{1}x_{2}...x_{n} and Y = y_{1}y_{2}...y_{m} be two distinct strings. X is lexicographically larger than Y if and only if Y is a prefix of X or x_{j} > y_{j} where j is the smallest integer such that x_{j} \neq y_{j}.

Submission attempts (in order):
s = input()
K = int(input())
l = [chr(i) for i in range(97, 97+26)]
selected = []
for i in l:
  if i in s:
    index = []
    for ind, j in enumerate(s):
      if i == j:
        index.append(ind)
    print(index)
    for j in index:
      for k in range(len(s)):
        if j+k == len(s):
          break
        selected.append(s[j:j+k+1])
    selected=list(set(selected))
    selected.sort()
    if len(selected)>=K:
      print(selected[K-1])
      break


s = input()
K = int(input())
l = [chr(i) for i in range(97, 97+26)]
selected = []
for i in l:
  if i in s:
    index = []
    for ind, j in enumerate(s):
      if i == j:
        index.append(ind)
    for j in index:
      temp = []
      for k in range(len(s)):
        if j+k == len(s) or len(temp)>=K:
          selected+= temp
          break
        temp.append(s[j:j+k+1])
    selected=list(set(selected))
    selected.sort()
    if len(selected)>=K:
      print(selected[K-1])
      break


s = input()
K = int(input())
l = [chr(i) for i in range(97, 97+26)]
selected = []
for i in l:
  if i in s:
    index = []
    for ind, j in enumerate(s):
      if i == j:
        index.append(ind)
    for j in index:
      for k in range(K):
        if j+k == len(s):
          break
        selected.append(s[j:j+k+1])
    selected=list(set(selected))
    selected.sort()
    if len(selected)>=K:
      print(selected[K-1])
      break


Verdicts: Wrong Answer

Wrong Answer

Accepted
Problem: You are given a string s. Among the **different** substrings of s, print the K-th lexicographically smallest one.  A substring of s is a string obtained by taking out a non-empty contiguous part in s. For example, if s = `ababc`, `a`, `bab` and `ababc` are substrings of s, while `ac`, `z` and an empty string are not. Also, we say that substrings are different when they are different as strings.  Let X = x_{1}x_{2}...x_{n} and Y = y_{1}y_{2}...y_{m} be two distinct strings. X is lexicographically larger than Y if and only if Y is a prefix of X or x_{j} > y_{j} where j is the smallest integer such that x_{j} \neq y_{j}.

Submission attempts (in order):
import sys

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

chars = sorted(set(s))
n = len(s)

for j in range(min(3, len(chars))):
  ans = chars[j]
  K -= 1
  if K == 0:
    print(ans)
    break
  while True:
    places = []
    index = -1
    while True:
      index = s.find(ans, index+1)
      if index == -1:
        break
      places.append(index)

    print(places, ans, K)
    if len(ans) == len(s) - places[0]:
      break

    next_char = 'z' * 6
    for k in places:
      if k >= len(s)-len(ans):
        break
      next_char = min(next_char, s[k+len(ans)])

    ans += next_char
    K -= 1
    if K == 0:
      print(ans)
      sys.exit()

import sys

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

chars = sorted(set(s))
n = len(s)

for j in range(min(3, len(chars))):
  ans = chars[j]
  K -= 1
  if K == 0:
    print(ans)
    break
  while True:
    places = []
    index = -1
    while True:
      index = s.find(ans, index+1)
      if index == -1:
        break
      places.append(index)
      
    if len(ans) == len(s) - places[0]:
      break

    next_char = 'z' * 6
    for k in places:
      if k >= len(s)-len(ans):
        break
      next_char = min(next_char, s[k+len(ans)])

    ans += next_char
    K -= 1
    if K == 0:
      print(ans)
      sys.exit()

Verdicts: Wrong Answer

Accepted
Problem: You are given a string s. Among the **different** substrings of s, print the K-th lexicographically smallest one.  A substring of s is a string obtained by taking out a non-empty contiguous part in s. For example, if s = `ababc`, `a`, `bab` and `ababc` are substrings of s, while `ac`, `z` and an empty string are not. Also, we say that substrings are different when they are different as strings.  Let X = x_{1}x_{2}...x_{n} and Y = y_{1}y_{2}...y_{m} be two distinct strings. X is lexicographically larger than Y if and only if Y is a prefix of X or x_{j} > y_{j} where j is the smallest integer such that x_{j} \neq y_{j}.

Submission attempts (in order):
# -*- coding: utf-8 -*-
def inpl(): return map(int, input().split())
N = int(input())
balls = [input().replace(" ", "") for _ in range(2*N)] + ["END"]

B = ["END"] + ["B{}".format(i) for i in range(1, N+1)[::-1]]
W = ["END"] + ["W{}".format(i) for i in range(1, N+1)[::-1]]

ans = 0
for i in range(2*N):
    b = balls.index(B[-1])
    w = balls.index(W[-1])
    if b < w:
        if b != i:
            ans += b-i
            del balls[b]
            balls.insert(i, B[-1])
        B.pop()
    else:
        if w != i:
            ans += w-i
            del balls[w]
            balls.insert(i, W[-1])
        W.pop()

print(ans)

# -*- coding: utf-8 -*-
def inpl(): return map(int, input().split())
S = input()
K = int(input())
ans = set()

for s in sorted(set(S)):
    if len(ans) >= K:
        break
    for i in range(len(S)):
        if S[i] == s:
            for k in range(K):
                ans.add(S[i:i+k+1])

print(sorted(ans)[K-1])

Verdicts: Runtime Error

Accepted
Problem: You are given a string s. Among the **different** substrings of s, print the K-th lexicographically smallest one.  A substring of s is a string obtained by taking out a non-empty contiguous part in s. For example, if s = `ababc`, `a`, `bab` and `ababc` are substrings of s, while `ac`, `z` and an empty string are not. Also, we say that substrings are different when they are different as strings.  Let X = x_{1}x_{2}...x_{n} and Y = y_{1}y_{2}...y_{m} be two distinct strings. X is lexicographically larger than Y if and only if Y is a prefix of X or x_{j} > y_{j} where j is the smallest integer such that x_{j} \neq y_{j}.

Submission attempts (in order):
from numba import njit
s=input()
K=int(input())

N=len(s)
L=[]
@njit
def solve():
    for i in range(N):
        for j in range(i,N):
            L.append(s[i:j+1])
L=sorted(list(set(L)))
print(L[K-1])


N=len(s)
L=[]
flag=False
for a in abc:
    for i in range(N):
        if s[i]==a:
            L.append(s[i:])
            
    if flag:
        break
L=sorted(L)
#print(L)
ans=[]
for l in L:
    n=len(l)
    for i in range(n):
        t=l[:i+1]
        if t not in ans:
            ans.append(l[:i+1])
    if len(ans)>=K:
        break
#print(ans)
print(ans[K-1])




s=input()
K=int(input())
abc=list("abcdefghijklmnopqrstuvwxyz")

N=len(s)
L=[]
flag=False
for a in abc:
    for i in range(N):
        if s[i]==a:
            L.append(s[i:])
            
    if flag:
        break
L=sorted(L)
#print(L)
ans=[]
for l in L:
    n=len(l)
    for i in range(n):
        t=l[:i+1]
        if t not in ans:
            ans.append(l[:i+1])
    if len(ans)>=K:
        break
#print(ans)
print(ans[K-1])




Verdicts: Runtime Error

Runtime Error

Accepted
Problem: You are given a string s. Among the **different** substrings of s, print the K-th lexicographically smallest one.  A substring of s is a string obtained by taking out a non-empty contiguous part in s. For example, if s = `ababc`, `a`, `bab` and `ababc` are substrings of s, while `ac`, `z` and an empty string are not. Also, we say that substrings are different when they are different as strings.  Let X = x_{1}x_{2}...x_{n} and Y = y_{1}y_{2}...y_{m} be two distinct strings. X is lexicographically larger than Y if and only if Y is a prefix of X or x_{j} > y_{j} where j is the smallest integer such that x_{j} \neq y_{j}.

Submission attempts (in order):
def cut_kouho(s, char):
    ret = []
    for i in range(s.count(char)):
        ind = s.find(char)
        ret.append(s[ind:])
        if ind == len(s) -1 :
            break
        s = s[ind+1:]
    print(ret)
    return ret

s = input()
k = int(input())

if len(s) < 51 and False:
    tmp = []
    for i in range(len(s)):
        for j in range(i,len(s)):
            tmp.append(s[i:j+1])
    a = list(set(tmp))
    a.sort()
    print(a[k-1])
else:
    char = list(set(list(s)))
    char.sort()
    #print(char)
    nokori = k
    for c in char:
        kouho = cut_kouho(s, c)
        
        kouho = [i[:min(nokori, len(i))] for i in kouho]
        
        
        tmp = []
        for string in kouho:
            for i in range(len(string)):
                for j in range(i,len(string)):
                    tmp.append(string[i:j+1])
        a = list(set(tmp))
        a.sort()
        if len(a) >= nokori:
            print(a[nokori-1])
            exit()
        else:
            nokori -= len(a)


def cut_kouho(s, char):
    ret = []
    for i in range(s.count(char)):
        ind = s.find(char)
        ret.append(s[ind:])
        if ind == len(s) -1 :
            break
        s = s[ind+1:]
    #print(ret)
    return ret

s = input()
k = int(input())

if len(s) < 51 and False:
    tmp = []
    for i in range(len(s)):
        for j in range(i,len(s)):
            tmp.append(s[i:j+1])
    a = list(set(tmp))
    a.sort()
    print(a[k-1])
else:
    char = list(set(list(s)))
    char.sort()
    #print(char)
    nokori = k
    for c in char:
        kouho = cut_kouho(s, c)
        
        kouho = [i[:min(nokori, len(i))] for i in kouho]
        
        
        tmp = []
        for string in kouho:
            for i in range(len(string)):
                tmp.append(string[:i+1])
        a = list(set(tmp))
        a.sort()
        #print(a)
        if len(a) >= nokori:
            print(a[nokori-1])
            exit()
        else:
            nokori -= len(a)

Verdicts: Wrong Answer

Accepted
Problem: You are given a string s. Among the **different** substrings of s, print the K-th lexicographically smallest one.  A substring of s is a string obtained by taking out a non-empty contiguous part in s. For example, if s = `ababc`, `a`, `bab` and `ababc` are substrings of s, while `ac`, `z` and an empty string are not. Also, we say that substrings are different when they are different as strings.  Let X = x_{1}x_{2}...x_{n} and Y = y_{1}y_{2}...y_{m} be two distinct strings. X is lexicographically larger than Y if and only if Y is a prefix of X or x_{j} > y_{j} where j is the smallest integer such that x_{j} \neq y_{j}.

Submission attempts (in order):
s=input()
K=int(input())
D=[]
for i in range(len(s)):
  for j in range(len(s)-i-1):
    D.append(s[i:i+j+1])
D = sorted(list(set(D)))
print(D[K-1])

s=input()
K=int(input())
D=[]
for i in range(len(s)+1):
  for j in range(min(len(s)-i,5)):
    D.append(s[i:i+j+1])
D = sorted(list(set(D)))
#print(D)
print(D[K-1])

Verdicts: Runtime Error

Accepted
Problem: You are given a string s. Among the **different** substrings of s, print the K-th lexicographically smallest one.  A substring of s is a string obtained by taking out a non-empty contiguous part in s. For example, if s = `ababc`, `a`, `bab` and `ababc` are substrings of s, while `ac`, `z` and an empty string are not. Also, we say that substrings are different when they are different as strings.  Let X = x_{1}x_{2}...x_{n} and Y = y_{1}y_{2}...y_{m} be two distinct strings. X is lexicographically larger than Y if and only if Y is a prefix of X or x_{j} > y_{j} where j is the smallest integer such that x_{j} \neq y_{j}.

Submission attempts (in order):
s = input()
K = int(input())

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

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


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

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

sub = list(set(sub))
sub.sort()
print(sub[k+1])


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

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

sub = list(set(sub))
sub.sort()
print(sub[K-1])


Verdicts: Wrong Answer

Runtime Error

Accepted
Problem: You are given a string s. Among the **different** substrings of s, print the K-th lexicographically smallest one.  A substring of s is a string obtained by taking out a non-empty contiguous part in s. For example, if s = `ababc`, `a`, `bab` and `ababc` are substrings of s, while `ac`, `z` and an empty string are not. Also, we say that substrings are different when they are different as strings.  Let X = x_{1}x_{2}...x_{n} and Y = y_{1}y_{2}...y_{m} be two distinct strings. X is lexicographically larger than Y if and only if Y is a prefix of X or x_{j} > y_{j} where j is the smallest integer such that x_{j} \neq y_{j}.

Submission attempts (in order):
import collections
s = input()
K = int(input())

abc = collections.defaultdict(list)

for i in range(len(s)):
  for j in range(1,K+1):
    if i+j < len(s):
        abc[s[i:i+j]].append(i)


abc = list(abc.items())
abc.sort(key=lambda x: x[0])


print(abc[K][0])


import collections
s = input()
K = int(input())

abc = collections.defaultdict(list)

for i in range(len(s)):
    if i > len(s) - 5:
        abc[s[i:]].append(i)
    else:
        abc[s[i:i+5]].append(i)

abc = list(abc.items())
abc.sort(key=lambda x: x[0])


print(abc[0][0])


import collections
s = input()
K = int(input())

abc = collections.defaultdict(list)

for i in range(len(s)):
  for j in range(1,K+1):
    if i+j <= len(s):
        abc[s[i:i+j]].append(i)


abc = list(abc.items())
abc.sort(key=lambda x: x[0])

print(abc[K-1][0])


Verdicts: Runtime Error

Wrong Answer

Accepted
Problem: You are given a string s. Among the **different** substrings of s, print the K-th lexicographically smallest one.  A substring of s is a string obtained by taking out a non-empty contiguous part in s. For example, if s = `ababc`, `a`, `bab` and `ababc` are substrings of s, while `ac`, `z` and an empty string are not. Also, we say that substrings are different when they are different as strings.  Let X = x_{1}x_{2}...x_{n} and Y = y_{1}y_{2}...y_{m} be two distinct strings. X is lexicographically larger than Y if and only if Y is a prefix of X or x_{j} > y_{j} where j is the smallest integer such that x_{j} \neq y_{j}.

Submission attempts (in order):
s=input()
k=int(input())
l=[]
for i in range(len(s)-1):
  for j in range(i+1,min(len(s),i+5)):
    if s[i:j] not in l:
      l.append(s[i:j])
l.sort()
print(l[k-1])

s=input()
k=int(input())
l=[]
for i in range(len(s)):
  for j in range(i+1,min(len(s),i+k)+1):
    if s[i:j] not in l:
      l.append(s[i:j])
l.sort()
print(l[k-1])


Verdicts: Runtime Error

Accepted
Problem: You are given a string s. Among the **different** substrings of s, print the K-th lexicographically smallest one.  A substring of s is a string obtained by taking out a non-empty contiguous part in s. For example, if s = `ababc`, `a`, `bab` and `ababc` are substrings of s, while `ac`, `z` and an empty string are not. Also, we say that substrings are different when they are different as strings.  Let X = x_{1}x_{2}...x_{n} and Y = y_{1}y_{2}...y_{m} be two distinct strings. X is lexicographically larger than Y if and only if Y is a prefix of X or x_{j} > y_{j} where j is the smallest integer such that x_{j} \neq y_{j}.

Submission attempts (in order):
def sub_k(string, I, K):
    temp = []
    for i in range(len(string)):
        if i-1+I == len(string):
            continue
        temp.append(string[i:i+I])
    return sorted(set(temp))[0:K]

def main(string, K):
    for i in range(K):
        print(sub_k(string, i+1, K-i))

string = input()
K = int(input())
main(string, K)

def sub_k(string, I, K):
    temp = []
    for i in range(len(string)):
        if i-1+I == len(string):
            continue
        temp.append(string[i:i+I])
    return sorted(set(temp))[0:K]

def main(string, K):
    for i in range(K):
        print(sub_k(string, i+1, K-i))

string = input()
K = input()
main(string, K)

def sub_k(string, I, K):
    temp = []
    for i in range(len(string)):
        if i-1+I == len(string):
            break
        temp.append(string[i:i+I])
    return sorted(set(temp))[0:K]

def main(string, K):
    result = []
    for i in range(K):
        result += sub_k(string, i+1, K-i)
    print(sorted(set(result))[K-1])

string = input()
K = int(input())
main(string, K)

Verdicts: Wrong Answer

Runtime Error

Accepted
Problem: You are given a string s. Among the **different** substrings of s, print the K-th lexicographically smallest one.  A substring of s is a string obtained by taking out a non-empty contiguous part in s. For example, if s = `ababc`, `a`, `bab` and `ababc` are substrings of s, while `ac`, `z` and an empty string are not. Also, we say that substrings are different when they are different as strings.  Let X = x_{1}x_{2}...x_{n} and Y = y_{1}y_{2}...y_{m} be two distinct strings. X is lexicographically larger than Y if and only if Y is a prefix of X or x_{j} > y_{j} where j is the smallest integer such that x_{j} \neq y_{j}.

Submission attempts (in order):
s = input().rstrip()
k = int(input())
result = []
for i in range(len(s)):
  for j in range(i+1,i+k):
    if i+k <= len(s):
      result.append(s[i:j])
result.sort()
print(result[k-1])

s = input().rstrip()
k = int(input())
result = []
for i in range(len(s)):
  for j in range(i+1,i+k+1):
    if j <= len(s) and s[i:j] not in result:
      result.append(s[i:j])
result.sort()
print(result[k-1])

Verdicts: Runtime Error

Accepted
Problem: You are given a string s. Among the **different** substrings of s, print the K-th lexicographically smallest one.  A substring of s is a string obtained by taking out a non-empty contiguous part in s. For example, if s = `ababc`, `a`, `bab` and `ababc` are substrings of s, while `ac`, `z` and an empty string are not. Also, we say that substrings are different when they are different as strings.  Let X = x_{1}x_{2}...x_{n} and Y = y_{1}y_{2}...y_{m} be two distinct strings. X is lexicographically larger than Y if and only if Y is a prefix of X or x_{j} > y_{j} where j is the smallest integer such that x_{j} \neq y_{j}.

Submission attempts (in order):
s = input()
k = int(input())

x = ['a', 'b', 'ab', 'ba', 'aba']
a=[]
m = min(s)
for i in range(len(s)):
    a.append(s[i])
    if s[i]==m:
        a.append(s[i:min(len(s)-1,i+2)])
        a.append(s[i:min(len(s)-1,i+3)])
        a.append(s[i:min(len(s)-1,i+4)])
        a.append(s[i:min(len(s)-1,i+5)])
a=list(set(a))
a.sort()
if a[0]=='':
    a=a[1:]
print(a[k-1])

s = input()
k = int(input())

x = ['a', 'b', 'ab', 'ba', 'aba']
a=[]
m = min(s)
for i in range(len(s)):
    a.append(s[i])
    a.append(s[i:min(len(s),i+2)])
    a.append(s[i:min(len(s),i+3)])
    a.append(s[i:min(len(s),i+4)])
    a.append(s[i:min(len(s),i+5)])
a=list(set(a))
a.sort()
if a[0]=='':
    a=a[1:]
print(a[k-1])

Verdicts: Runtime Error

Accepted
Problem: You are given a string s. Among the **different** substrings of s, print the K-th lexicographically smallest one.  A substring of s is a string obtained by taking out a non-empty contiguous part in s. For example, if s = `ababc`, `a`, `bab` and `ababc` are substrings of s, while `ac`, `z` and an empty string are not. Also, we say that substrings are different when they are different as strings.  Let X = x_{1}x_{2}...x_{n} and Y = y_{1}y_{2}...y_{m} be two distinct strings. X is lexicographically larger than Y if and only if Y is a prefix of X or x_{j} > y_{j} where j is the smallest integer such that x_{j} \neq y_{j}.

Submission attempts (in order):
from sys import stdin
from sys import setrecursionlimit
setrecursionlimit(10 ** 7)

s = stdin.readline().rstrip()
k = int(stdin.readline().rstrip())


finish = set()
S = len(s)

for i in range(S):
    for j in range(1,6):
        finish.add(s[i:i+j])

finish = list(finish)

print(finish[k-1])

s = input()
S = len(s)
k = int(input())

se = set()

def string(now,num):
    if now+num == s or num == k:
        return
    se.add(s[now:now+num+1])
    num += 1
    return string(now,num)

for i in range(S):
    string(i,0)

se = list(se)
se.sort()

print(se[k-1])

Verdicts: Wrong Answer

Accepted
Problem: We have a permutation of the integers from 1 through N, p_1, p_2, .., p_N. We also have M pairs of two integers between 1 and N (inclusive), represented as (x_1,y_1), (x_2,y_2), .., (x_M,y_M). AtCoDeer the deer is going to perform the following operation on p as many times as desired so that the number of i (1 ≤ i ≤ N) such that p_i = i is maximized:    * Choose j such that 1 ≤ j ≤ M, and swap p_{x_j} and p_{y_j}.  Find the maximum possible number of i such that p_i = i after operations.

Submission attempts (in order):
from subprocess import*
call(('pypy3','-c',))


n,m=map(int,input().split())
P=[i-1 for i in list(map(int,input().split()))]

class UnionFind():
    def __init__(self,num):
        self.n = num         
        self.parents = [-1 for i in range(self.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):
        xx=self.find(x)  
        yy=self.find(y)  
        if xx==yy:
            return     
        else:
            size_xx=abs(self.parents[xx]) 
            size_yy=abs(self.parents[yy]) 
            if size_xx>size_yy:
                xx,yy=yy,xx  

            self.parents[yy]+=self.parents[xx] 
            self.parents[xx]=yy   

    
    def size(self,x):
        xx=self.find(x)
        return abs(self.parents[xx])

    
    def same(self,x,y):
        return 1 if self.find(x)==self.find(y) else 0

    
    def members(self,x):
        xx=self.find(x)
        return [i for i in range(self.n) if self.find(i)==xx]
             

    
    def roots(self):
        return [i for i,x in enumerate(self.parents) if x < 0]
        

    
    def all_group_members(self):
        return {r:self.members(r) for r in self.roots()}

    
    def state_grouping(self):
        return list(self.all_group_members().values())


uf=UnionFind(n)
for i in range(m):
    a,b=map(int,input().split())
    a-=1;b-=1
    uf.union(a,b)
ans=0
for i in range(n):
    ans+= uf.same(i,P[i])
print(ans)

Verdicts: Time Limit Exceeded

Accepted
Problem: We have a permutation of the integers from 1 through N, p_1, p_2, .., p_N. We also have M pairs of two integers between 1 and N (inclusive), represented as (x_1,y_1), (x_2,y_2), .., (x_M,y_M). AtCoDeer the deer is going to perform the following operation on p as many times as desired so that the number of i (1 ≤ i ≤ N) such that p_i = i is maximized:    * Choose j such that 1 ≤ j ≤ M, and swap p_{x_j} and p_{y_j}.  Find the maximum possible number of i such that p_i = i after operations.

Submission attempts (in order):
from sys import stdin
nii=lambda:map(int,stdin.readline().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 same(self, x, y):
    return self.find(x) == self.find(y)

n,m=nii()
p=list(nii())

uf=UnionFind(n+1)

for i in range(m):
  x,y=nii()
  uf.union(x,y)

cnt=0
for i in range(n):
  if not uf.same(i,p[i]):
    cnt+=1

print(n-cnt)

from sys import stdin
nii=lambda:map(int,stdin.readline().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 same(self, x, y):
    return self.find(x) == self.find(y)

n,m=nii()
p=list(nii())

uf=UnionFind(n)

for i in range(m):
  x,y=nii()
  x-=1
  y-=1
  uf.union(x,y)

cnt=0
for i in range(n):
  if not uf.same(i,p[i]-1):
    cnt+=1

print(n-cnt)

Verdicts: Wrong Answer

Accepted
Problem: We have a permutation of the integers from 1 through N, p_1, p_2, .., p_N. We also have M pairs of two integers between 1 and N (inclusive), represented as (x_1,y_1), (x_2,y_2), .., (x_M,y_M). AtCoDeer the deer is going to perform the following operation on p as many times as desired so that the number of i (1 ≤ i ≤ N) such that p_i = i is maximized:    * Choose j such that 1 ≤ j ≤ M, and swap p_{x_j} and p_{y_j}.  Find the maximum possible number of i such that p_i = i after operations.

Submission attempts (in order):

n,m=map(int,input().split())
root=[i for i in range(n+1)]
rank=[0]*(n+1)
def find(x):
    if x==root(x):
        return x 
    else:
        return find(root[x])

def union(x,y):
    x=find(x)
    y=find(y)
    if rank[x]>rank[y]:
        root[y]=x 
    else rank[x]<rank[y]:
        root[x]=y
        if rank[x]==rank[y]:
            rank[y]+=1
def k_sub():
    
    p=list(map(int,input().split()))
    p=[-1]+p 
    for _ in range(m):
        x,y=map(int,input().split())
        union(x,y)
    d=[[] for i in range(n+1)]
    u=[[] for i in range(n+1)]
    for i in range(1,n+1):
        d[find[i]].append(i)
        u[find[i]].append(p[i])
    cnt=0
    for i in range(n):
        for candidate in d[i]:
            if candidate in u[i]:
                cnt+=1
    print(cnt)
if __name__=='__main__':
    k_sub()
    

n,m=map(int,input().split())
p=list(map(int,input().split()))
uf={i:i for i in range(1,n+1)}
def get_p(i):
    lst=[]
    while uf[i]!=i:
        lst.append(i)
        i=uf[i]
    for l in lst[:-1]:
        uf[l]=i 
    return i 

def unite(i,j):
    uf[get_p(i)]=get_p(j)
def same(i,j):
    return get_p(i)==get_p(j)

def main():
    for i in range(m):
        x,y=map(int,input().split())
        unite(x,y)
    cnt=0
    for i in range(1,n+1):
        cnt+=same(p[i-1],uf[i])
    print(cnt)    
if __name__=='__main__':
    main()  

Verdicts: Runtime Error

Accepted
Problem: We have a permutation of the integers from 1 through N, p_1, p_2, .., p_N. We also have M pairs of two integers between 1 and N (inclusive), represented as (x_1,y_1), (x_2,y_2), .., (x_M,y_M). AtCoDeer the deer is going to perform the following operation on p as many times as desired so that the number of i (1 ≤ i ≤ N) such that p_i = i is maximized:    * Choose j such that 1 ≤ j ≤ M, and swap p_{x_j} and p_{y_j}.  Find the maximum possible number of i such that p_i = i after operations.

Submission attempts (in order):
class UnionFind():
	def __init__(self, size):
		self.table = [-1 for _ in range(size)]

	def find(self, x):
		while self.table[x] >= 0:
			x = self.table[x]
		return x

	def union(self, x, y):
		s1 = self.find(x)
		s2 = self.find(y)
		if s1 == s2:
			return
		elif:
			if self.table[s1] > self.table[s2]:
				self.table[s2] = s1
			elif self.table[s1] < self.table[s2]:
				self.table[s1] = s2
			else:
				self.table[s1] = s2
				self.table[s2] -= 1


n, m = map(int, input().split())
p = list(map(int, input().split()))

uf = UnionFind(n)

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

ans = 0
for i, pp in enumerate(p):
	if pp - 1 == i:
		ans += 1
	elif uf.find(pp - 1) == uf.find(i):
		ans += 1

print(ans)

class UnionFind():
	def __init__(self, size):
		self.table = [-1 for _ in range(size)]

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

	def union(self, x, y):
		s1 = self.find(x)
		s2 = self.find(y)
		if s1 == s2:
			return
		else:
			st1 = self.table[s1]
			st2 = self.table[s2]
			if st1 > st2:
				self.table[s2] = s1
			elif st1 < st2:
				self.table[s1] = s2
			else:
				self.table[s1] = s2
				self.table[s2] -= 1


n, m = map(int, input().split())
p = list(map(int, input().split()))

uf = UnionFind(n)

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

ans = 0
for i, pp in enumerate(p):
	if pp - 1 == i:
		ans += 1
	elif uf.find(pp - 1) == uf.find(i):
		ans += 1

print(ans)


Verdicts: Runtime Error

Accepted