Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
an = list(map(int, input().split()))
sum = 0
for k in range(len(an) - 1):
    i = k
    for i in range(len(an) - k - 1):
        sum += (an[k] * an[k + i + 1])
        if sum >= (10 ** 9 + 7):
            sum %= (10 ** 9 + 7)
print(sum)

Fixed submission:
p = (10 ** 9 + 7) * 2
N = int(input())
A = [int(x) for x in input().split()]
A2 = [(x ** 2) % p for x in A]
result = (((sum(A) % p) ** 2 - sum(A2) % p) % p) / 2
print(int(result))

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
an = list(map(int, input().split()))
sum = 0
for k in range(len(an) - 1):
    i = k
    for i in range(len(an) - k - 1):
        sum += (an[k] * an[k + i + 1])
    sum %= (10 ** 9 + 7)
print(sum)

Fixed submission:
p = (10 ** 9 + 7) * 2
N = int(input())
A = [int(x) for x in input().split()]
A2 = [(x ** 2) % p for x in A]
result = (((sum(A) % p) ** 2 - sum(A2) % p) % p) / 2
print(int(result))

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
an = list(map(int, input().split()))
sum = 0
for k in range(len(an) - 1):
    i = k
    for i in range(len(an) - k - 1):
        sum += (an[k] * an[k + i + 1])
        sum %= (10 ** 9 + 7)
print(sum)

Fixed submission:
p = (10 ** 9 + 7) * 2
N = int(input())
A = [int(x) for x in input().split()]
A2 = [(x ** 2) % p for x in A]
result = (((sum(A) % p) ** 2 - sum(A2) % p) % p) / 2
print(int(result))
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
shin = 0
while n > 0:
    shin += (n % 10)
    n //= 10
if shin % 9 == 0:
    print('Yes')
else:
    print('No')

Fixed submission:
n = list(input())
shin = 0
for i in n:
    shin += int(i)
if shin % 9 == 0:
    print('Yes')
else:
    print('No')
Buggy python submission (verdict: Time Limit Exceeded):
X, K, D = map(int, input().split())
if X == D:
    if K % 2 != 0:
        print('0')
    else:
        print(D)
else:
    if K * D < X:
        print(X - K * D)
    else:
        for i in range(K):
            if X >= 0:
                X = X - D
            else:
                X = X + D
        print(abs(X))

Fixed submission:
X, K, D = map(int, input().split())
X = abs(X)
if X == D:
    if K % 2 != 0:
        print('0')
    else:
        print(D)
else:
    span = X // D
    if span < K:
        if (K - span) % 2 == 0:
            print(X % D)
        else:
            print(abs(X % D - D))
    else:
        print(X - K * D)

Buggy python submission (verdict: Time Limit Exceeded):
X, K, D = map(int, input().split())
if X == D:
    if K % 2 != 0:
        print('0')
    else:
        print(D)
else:
    for i in range(K):
        if X >= 0:
            X = X - D
        else:
            X = X + D
    print(abs(X))

Fixed submission:
X, K, D = map(int, input().split())
X = abs(X)
if X == D:
    if K % 2 != 0:
        print('0')
    else:
        print(D)
else:
    span = X // D
    if span < K:
        if (K - span) % 2 == 0:
            print(X % D)
        else:
            print(abs(X % D - D))
    else:
        print(X - K * D)
Buggy python submission (verdict: Time Limit Exceeded):
k = int(input())
s = 7
num = 1
if k == 2:
    print(-1)
else:
    while 1:
        s %= k
        if s == 0:
            print(num)
            break
        num += 1
        s *= 10
        s += 7


Fixed submission:
k = int(input())
s = 7
num = 1
res = True
if k == 2:
    print(-1)
else:
    for i in range(k):
        s %= k
        if s == 0:
            res = False
            print(num)
            break
        num += 1
        s *= 10
        s += 7
    if res:
        print(-1)
Buggy python submission (verdict: Runtime Error):
import math
def combinations_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
def main():
  N, M = map(int, input().split())
  if N == 1 and M == 1:
    print(0)
  elif N == 1:
    print(combinations_count(M, 2))
  elif M == 1:
    print(combinations_count(N, 2))
  else:
    print(combinations_count(N, 2) + combinations_count(M, 2))
if __name__ == '__main__':
  	main()

Fixed submission:
import math
def combinations_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
def main():
  N, M = map(int, input().split())
  if N == 1 and M == 1:
    print(0)
  elif N == 0 or N == 1:
    print(combinations_count(M, 2))
  elif M == 0 or M == 1:
    print(combinations_count(N, 2))
  else:
    print(combinations_count(N, 2) + combinations_count(M, 2))
if __name__ == '__main__':
  	main()
Buggy python submission (verdict: Time Limit Exceeded):
import sys
import bisect
from functools import lru_cache
from collections import defaultdict
inf = float('inf')
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10**6)
def input(): return sys.stdin.readline().rstrip()
def read():
  return int(readline())
def reads():
  return map(int, readline().split())
n=read()
a=list(reads())
num=0
l=len(a)
for i in range(l-1):
    for j in range(i+1,l):
        num+=a[i]*a[j]
num%=10**9+7
print(num)

Fixed submission:
import sys
import bisect
from functools import lru_cache
from collections import defaultdict
inf = float('inf')
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10**6)
def input(): return sys.stdin.readline().rstrip()
def read():
  return int(readline())
def reads():
  return map(int, readline().split())
n=read()
a=list(reads())
num=0
l=len(a)
ls=[]
k=0
for i in range(l-1,0,-1):
    k+=a[i]
    ls.append(k)
for i in range(l-1):
    num+=a[i]*ls[-i-1]
num%=10**9+7
print(num)
Buggy python submission (verdict: Runtime Error):
import sys
import bisect
from functools import lru_cache
from collections import defaultdict
inf = float('inf')
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10**6)
def input(): return sys.stdin.readline().rstrip()
def read():
  return int(readline())
def reads():
  return map(int, readline().split())
K=reads()
mod=7//K
index=1
dic=defaultdict(int)
while(True):
    if mod==0:
        print(index)
        exit
    if dic[mod]==1:
        print(-1)
        exit
    dic[mod]=1
    mod=mod*10+7
    i+=1
    

Fixed submission:
import sys
import bisect
from functools import lru_cache
from collections import defaultdict
inf = float('inf')
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10**6)
def input(): return sys.stdin.readline().rstrip()
def read():
  return int(readline())
def reads():
  return map(int, readline().split())
K=read()
mod=7%K
index=1
dic=defaultdict(int)
while(True):
    if mod==0:
        print(index)
        exit()
    if dic[mod]==1:
        print(-1)
        exit()
    dic[mod]=1
    mod=mod*10+7
    mod%=K
    index+=1
Buggy python submission (verdict: Runtime Error):
a,b=input().split()
a=int(a)
b=Decimal(b)
print(int(a*b))

Fixed submission:
from decimal import *
a,b=input().split()
a=int(a)
b=Decimal(b)
print(int(a*b))

Buggy python submission (verdict: Runtime Error):
import sys
from functools import lru_cache
 
#read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10**6)
def read():
  return int(readline())
def reads():
  return map(int, readline().split())
def mp(arg):
  return map(int,arg.split())
n=read()
res=1
mod=10**18
a=list(reads())
if 0 in a:
  print("0")
  exit()
for i in range(n):   
    res*=a[i]
    print(res)
    if res>mod:
        print("-1")
        exit()
print(res)

Fixed submission:
from decimal import *
a,b=input().split()
a=int(a)
b=Decimal(b)
print(int(a*b))
Buggy python submission (verdict: Time Limit Exceeded):
t=input()
check=False
res=0
l=len(t)
res=""
for i in range(len(t)):
  if t[i]=="?":
      res+="D"
  else:res+=t[i]
print(res)
  

Fixed submission:
t=input()
check=False
print(t.replace('?', 'D'))
Buggy python submission (verdict: Runtime Error):
import sys
from functools import lru_cache
import random
#read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10**6)
def input(): return sys.stdin.readline().rstrip()
def read():
  return int(readline())
def reads():
  return map(int, readline().split())
dic={}
n=read()
s=[]
for i in range(n):
  s.append(input())
for i in range(5*10**2):
  count=0
  for j in range(min(10**3,n)):
    b=random.randrange(0,max(len(s[j])-99,1))
    for k in range(b,min(len(s[j]),100+b)):
      if s[j][k]=="(":
              count+=1
      elif s[j][k]==")":
              count-=1
      else:raise Exception
  try:
    dic[count]+=1
  except:
    dic[count]=1
#print(dic)
try:
  if dic[0]>10:
    print("Yes")
  else:
    print("No")
except:print("No")

Fixed submission:
import sys
from functools import lru_cache
 
#read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10**6)
def read():
  return int(readline())
def reads():
  return map(int, readline().split())
def cmb(n, r):
    if (r < 0) or (n < r):
        return 0
    r = min(r, n - r)
    return fact[n] * factinv[r] * factinv[n-r] % mod
n,m,k=reads()
N = n-1 # N は必要分だけ用意する
mod =  998244353
fact = [1, 1]  # fact[n] = (n! mod p)
factinv = [1, 1]  # factinv[n] = ((n!)^(-1) mod p)
inv = [0, 1]  # factinv 計算用

for i in range(2, N + 1):
    fact.append((fact[-1] * i) % mod)
    inv.append((-inv[mod % i] * (mod // i)) % mod)
    factinv.append((factinv[-1] * inv[-1]) % mod)

mod=998244353
numbers=[m]
for i in range(n-1):
  numbers.append(numbers[i]*(m-1)%mod)
ans=0
for i in range(k+1):
  ans+=numbers[n-i-1]*cmb(n-1,i)
ans%=mod
#print(numbers)
print(ans)
Buggy python submission (verdict: Runtime Error):
n,m=map(int,input().split())
a=0
a+=map(int,input().split())
if(n>a):
  print("{}",n-a)
else:
  print("-1",

Fixed submission:
n,m=map(int,input().split())
a=list(map(int,input().split()))
p=0
for i in a:
      p+=i
if(n>=p):
          print("{}".format(n-p))
else:
          print("-1")
Buggy python submission (verdict: Time Limit Exceeded):
import sys
from functools import lru_cache
sys.setrecursionlimit(10**6)
#read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

def read():
  return int(readline())
def reads():
  return map(int, readline().split())
def mp(arg):
  return map(int,arg.split())

n,m=reads()
link=[[]for i in range(n)]
for i in range(m):
  x,y=reads()
  link[x-1].append(y-1)
def dp(k):
  res=0
  for ele in link[k]:
    res=max(res,dp(ele)+1)
  return res
res=1
for i in range(n)  :
  res=max(res,dp(i))
print(res)

Fixed submission:
import sys
from functools import lru_cache
sys.setrecursionlimit(10**6)
#read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

def read():
  return int(readline())
def reads():
  return map(int, readline().split())
def mp(arg):
  return map(int,arg.split())

n,m=reads()
link=[[]for i in range(n)]
for i in range(m):
  x,y=reads()
  link[x-1].append(y-1)
#print(link)
@lru_cache(None)
def dp(k):
  res=0
  for ele in link[k]:
    res=max(res,dp(ele)+1)
  return res
res=1
for i in range(n)  :
  res=max(res,dp(i))
print(res)
Buggy python submission (verdict: Runtime Error):
n=int(input())
res=0
a1=int(input())
if a1!=0:
  print(-1)
  exit()
a2=int(input())
for  i in range(n-2):
  if a2-a1>1:
    print("-1")
    exit()
  if a2<=a1:
    res+=a1
  a1=a2
  a2=int(input())
if a2-a1>1:
    print("-1")
    exit()
if a2<=a1:
    res+=a1
res+=a2
print(res)

Fixed submission:
n=int(input())
res=0
a1=int(input())
if a1!=0:
  print(-1)
  exit()
try:a2=int(input())
except:
      print("0")
      exit()
for  i in range(n-2):
  if a2-a1>1:
    print("-1")
    exit()
  if a2<=a1:
    res+=a1
  a1=a2
  a2=int(input()) 
if a2-a1>1:
    print("-1")
    exit()
if a2<=a1:
    res+=a1
res+=a2
print(res)

Buggy python submission (verdict: Runtime Error):
n=int(input())
res=0
a1=int(input())
if a1!=0:
  print(-1)
  exit()
a2=int(input())
for  i in range(n-2):
  if a2-a1>1:
    print("-1")
    exit()
  if a2<=a1:
    res+=a1
  a1=a2
  a2=int(input())
if a2-a1>1:
    print("-1")
    exit()
res+=a2
print(res)

Fixed submission:
n=int(input())
res=0
a1=int(input())
if a1!=0:
  print(-1)
  exit()
try:a2=int(input())
except:
      print("0")
      exit()
for  i in range(n-2):
  if a2-a1>1:
    print("-1")
    exit()
  if a2<=a1:
    res+=a1
  a1=a2
  a2=int(input()) 
if a2-a1>1:
    print("-1")
    exit()
if a2<=a1:
    res+=a1
res+=a2
print(res)

Buggy python submission (verdict: Runtime Error):
n=int(input())
res=0
a1=int(input())
if a1!=0:
  print(-1)
  exit()
a2=int(input())
for  i in range(n-2):
  if a2-a1>1:
    print("-1")
    exit()
  if a2<=a1:
    res+=a1
  a1=a2
  a2=int(input())
res+=a2
print(res)

Fixed submission:
n=int(input())
res=0
a1=int(input())
if a1!=0:
  print(-1)
  exit()
try:a2=int(input())
except:
      print("0")
      exit()
for  i in range(n-2):
  if a2-a1>1:
    print("-1")
    exit()
  if a2<=a1:
    res+=a1
  a1=a2
  a2=int(input()) 
if a2-a1>1:
    print("-1")
    exit()
if a2<=a1:
    res+=a1
res+=a2
print(res)

Buggy python submission (verdict: Time Limit Exceeded):
n=int(input())
a=""
res=0
for i in range(n):
  a+=input()
if int(a[0])!=0:
  print(-1)
  exit()
for  i in range(n-1):
  a2=int(a[i+1])
  a1=int(a[i])
  if a2-a1>1:
    print("-1")
    exit()
  if a2<=a1:
    res+=a1
res+=int(a[n-1])
print(res)

Fixed submission:
n=int(input())
res=0
a1=int(input())
if a1!=0:
  print(-1)
  exit()
try:a2=int(input())
except:
      print("0")
      exit()
for  i in range(n-2):
  if a2-a1>1:
    print("-1")
    exit()
  if a2<=a1:
    res+=a1
  a1=a2
  a2=int(input()) 
if a2-a1>1:
    print("-1")
    exit()
if a2<=a1:
    res+=a1
res+=a2
print(res)
Buggy python submission (verdict: Runtime Error):
class unionfind():
    def __init__(self, n):
        self.parents = [-1 for i in range(n)]
    
    def find(self, a):
        if self.parents[a] < 0:
            return a
        self.parents[a] = self.find(a)
        return self.parents[a]

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

        self.parents[x] += self.parents[y]
        self.parents[y] = x
    def size(self):
        return len(list(filter(lambda x: x < 0, self.parents))) - 1

n, m = map(int, input().split())
uf = unionfind(n)
for i in range(m):
    tmp = list(map(int, input().split()))
    uf.union(tmp[0] - 1, tmp[1] - 1)

print(uf.size())


Fixed submission:
class unionfind():
    def __init__(self, n):
        self.parents = [-1 for i in range(n)]

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

    def union(self, x, y):
        x, y = self.find(x), self.find(y)
        if x == y:
            return
        if x < y:
            x, y = y, x
        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self):
        return len(list(filter(lambda x: x < 0, self.parents))) - 1

n, m = map(int, input().split())
uf = unionfind(n)
for i in range(m):
    tmp = list(map(int, input().split()))
    uf.union(tmp[0] - 1, tmp[1] - 1)

print(uf.size())
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
X = input()
MOD = len(list(filter(lambda x:x=='1', X)))

for i in range(N):
    c = 0
    tmp = X[:i] + ('1' if X[i] == '0' else '0') + X[i+1:]
    tmp = int(tmp, 2)
    while tmp:
        MOD = len(list(filter(lambda x:x=='1', format(tmp, 'b'))))
        tmp = tmp % MOD
        c += 1
    print(c)


Fixed submission:
N = int(input())
X = input()
tmp = bin(int(X, 2)).count("1")
big = int(X, 2) % (tmp + 1)
small = int(X, 2) % (tmp - 1) if tmp - 1 else 0

for i in range(N):
    ans = 0
    if X[i] == '1':
        if not tmp - 1:
            print(ans)
            continue

        c = small - pow(2, (N - i - 1), tmp - 1)
        c = c % (tmp - 1)
    else:
        c = big + pow(2, (N - i - 1), tmp + 1)
        c = c % (tmp + 1)
    ans += 1
    while c:
        MOD = bin(int(c)).count("1")
        c %= MOD
        ans += 1
    print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
def C(n, r):
    global MOD
    if (r < 0) or (n < r):
        return 0
    r = min(r, n - r)
    return fact[n] * factinv[r] * factinv[n-r] % MOD
    

K = int(input())
S = input()
N = len(S)

MOD = 10 ** 9 + 7
ans = 0

fact = [1, 1]
factinv = [1, 1]
inv = [0, 1]
 
for i in range(2, K + N + 1):
    fact.append((fact[-1] * i) % MOD)
    inv.append((-inv[MOD % i] * (MOD // i)) % MOD)
    factinv.append((factinv[-1] * inv[-1]) % MOD)

for i in range(0, K + 1):
    ans += C(i + N - 1, N - 1) * pow(25, i, MOD) * pow(26, K - i, MOD)
    ans %= MOD
print(ans)




Fixed submission:
def C(n, r):
    global MOD
    if (r < 0) or (n < r):
        return 0
    r = min(r, n - r)
    return fact[n] * factinv[r] * factinv[n-r] % MOD
    

K = int(input())
S = input()
N = len(S)

MOD = 10 ** 9 + 7
ans = 0

fact = [1, 1]
factinv = [1, 1]
inv = [0, 1]
 
for i in range(2, K + N + 1):
    fact.append((fact[-1] * i) % MOD)
    inv.append((-inv[MOD % i] * (MOD // i)) % MOD)
    factinv.append((factinv[-1] * inv[-1]) % MOD)

for i in range(0, K + 1):
    ans += C(i + N - 1, N - 1) * pow(25, i, MOD) * pow(26, K - i, MOD)
    ans %= MOD
print(ans)
Buggy python submission (verdict: Runtime Error):
N, Q = map(int, input().split())
class_yoti = [[] for i in range(2 * 10 ** 5 + 1)]
A, B, C, D = [], [], [], []
max_rate = float('inf')

for i in range(N):
    tmpa, tmpb = map(int, input().split())
    A.append(tmpa), B.append(tmpb)
    class_yoti[tmpb].append(tmpa)

for i in range(Q):
    tmpa, tmpb = map(int, input().split())
    C.append(tmpa - 1), D.append(tmpb)

for i in range(2 * 10 ** 5 + 1):
    if class_yoti[i]:
        class_yoti[i].sort()
        if max_rate > class_yoti[i][-1]:
            max_rate = class_yoti[i][-1]

for i in range(Q):
    tmp = C[i]
    class_yoti[B[tmp]].remove(A[tmp])
    if class_yoti[B[tmp]]:
        class_yoti[B[tmp]].sort()

    B[tmp] = D[i]
    class_yoti[D[i]].append(A[tmp])
    class_yoti[D[i]].sort()

    max_rate = float('inf')
    for j in range(2 * 10 ** 5 + 1):
        if class_yoti[j]:
            max_rate = min(class_yoti[j][-1], max_rate)

    print(max_rate)



Fixed submission:
from collections import Counter

N = int(input())
A = list(map(int, input().split()))

A.sort()

ans = 0
tmp = [True for i in range(A[-1] + 1)]

for i in range(N - 1):
    if tmp[A[i]]:
        if A[i + 1] != A[i]:
            ans += 1
        for j in range(A[i], A[-1] + 1, A[i]):
            tmp[j] = False
if tmp[A[-1]]:
    ans += 1
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
A = list(map(int, input().split()))

ans = 1
for i in A:
    ans *= i

print(-1 if ans > 10 ** 18 else ans)


Fixed submission:
N = int(input())
A = list(map(int, input().split()))

if 0 in A:
    print(0)
    exit()

ans = 1
for i in A:
    ans *= i
    
    if ans > 10 ** 18:
        print(-1)
        exit()

print(ans)
Buggy python submission (verdict: Runtime Error):
N = int(input())
A, B = [], []
for i in range(N):
    tm, p = map(int, input().split())
    A.append(tm), B.append(p)
A.sort(), B.sort()

a = A[N // 2] * 2 if N % 2 == 1 else A[N // 2] + A[N // 2 + 1]
b = B[N // 2] * 2 if N % 2 == 1 else B[N // 2] + B[N // 2 + 1]
print((b - a) // 2 + 1)


Fixed submission:
N = int(input())
A, B = [], []
for i in range(N):
    tm, p = map(int, input().split())
    A.append(tm), B.append(p)
A.sort(), B.sort()

a = A[N // 2] * 2 if N % 2 == 1 else A[N // 2 - 1] + A[N // 2]
b = B[N // 2] * 2 if N % 2 == 1 else B[N // 2 - 1] + B[N // 2]

print((b - a) // 2 + 1 if N % 2 == 1 else b - a + 1)
Buggy python submission (verdict: Runtime Error):
N, M = map(int, input().split())

lists = [False for i in range(N)]
lists[0] = True
a = N - 1
dicts = {i:[] for i in range(N)}
d = deque([0])

for i in range(M):
    tmpa, tmpb = map(lambda x: int(x) - 1, input().split()) 
    dicts[tmpa] += [tmpb]
    dicts[tmpb] += [tmpa]

print('Yes')
while a > 0:
    tmp = d.popleft()
    for i in dicts[tmp]:
        if not lists[i]:
            if tmp != 0:
                lists[i] = tmp
            else:
                lists[i] = -1
            d.append(i)
            a -= 1
    
for i in range(1, N):
    print(lists[i] + 1 if lists[i] != -1 else 1)


Fixed submission:
from collections import deque
class UnionFind:
    def __init__(self, N):
        self.parents = [-1 for i in range(N)]
        self.unit = [0 for i in range(N)]
    
    def find(self, x):
        if self.parents[x] < 0:
            return x
        self.parents[x] = self.find(self.parents[x])
        return self.parents[x]
    
    def union(self, x, y):
        x, y = self.find(x), self.find(y)
        if x == y:
            return
        if x > y:
            x, y = y, x
            
        self.parents[x] += self.parents[y]
        self.parents[y] = x

N, M = map(int, input().split())
uf = UnionFind(N)


lists = [False for i in range(N)]
lists[0] = True
a = N - 1
dicts = {i:[] for i in range(N)}
d = deque([0])

for i in range(M):
    tmpa, tmpb = map(lambda x: int(x) - 1, input().split()) 
    dicts[tmpa] += [tmpb]
    dicts[tmpb] += [tmpa]
    uf.union(tmpa, tmpb)

if -uf.parents[0] == N:
    print('Yes')
    while a > 0:
        tmp = d.popleft()
        for i in dicts[tmp]:
            if not lists[i]:
                if tmp != 0:
                    lists[i] = tmp
                else:
                    lists[i] = -1
                d.append(i)
                a -= 1
    
    for i in range(1, N):
        print(lists[i] + 1 if lists[i] != -1 else 1)
else:
    print('No')
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
A = list((i, j) for i, j in enumerate(map(int, input().split())))
un = sorted(A, reverse=True)

ans = 0

for i in range(N):
    un = un[0:-1]
    ans += len(tuple(filter(lambda x:x[0]-i == x[1]+A[i][1], un)))

print(ans)

Fixed submission:
N = int(input())
A = list((i, j) for i, j in enumerate(map(int, input().split())))
dictL = {}
dictR = {}

ans = 0
for i in range(N):
    l, r = A[i][1] + A[i][0], A[i][0] - A[i][1]
    if l not in dictL:
        dictL[l] = 1
    else:
        dictL[l] += 1
    if r not in dictR:
        dictR[r] = 1
    else:
        dictR[r] += 1

for i in dictL.keys():
    if i in dictR.keys():
        ans += dictL[i] * dictR[i]
        
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
A = list(map(int, input().split()))

ans = 0
for i, a in enumerate(A):
    for j in range(i + 1, N):
        if j - i == a + A[j]:
            ans += 1
            
print(ans)


Fixed submission:
N = int(input())
A = list((i, j) for i, j in enumerate(map(int, input().split())))
dictL = {}
dictR = {}

ans = 0
for i in range(N):
    l, r = A[i][1] + A[i][0], A[i][0] - A[i][1]
    if l not in dictL:
        dictL[l] = 1
    else:
        dictL[l] += 1
    if r not in dictR:
        dictR[r] = 1
    else:
        dictR[r] += 1

for i in dictL.keys():
    if i in dictR.keys():
        ans += dictL[i] * dictR[i]
        
print(ans)