Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Runtime Error):
N,W = map(int,input().split())

items = [tuple(map(int,input().split())) for i in range(N)]

from collections import defaultdict
dp = defaultdict(int) # w -> v
dp[0] = 0

for w,v in items:
  for tw,tv in list(dp.items()):
    if tw+w <= W:
      dp[tw+w] = max(tv+v, dp[tw+w])

print(max(dp.values()))

Fixed submission:
f=lambda:map(int,input().split())
N,T=f()
q=list(f())
print(T+sum(min(q[i+1]-q[i],T) for i in range(N-1)))
Buggy python submission (verdict: Runtime Error):
x = 1

for i,d in zip(reversed(range(len(D))), reversed(D)):
  if Q[-1][0] == i:
    result[Q[-1][1]] = P[i] >= x
    Q.pop()
    if not Q:
      break

  if abs(x-d) < x:
    x += d

for r in result:
  print('YES' if r else 'NO')

Fixed submission:
N,start = map(int,input().split())
D = list(map(int,input().split()))
input()
Q = list(map(int,input().split()))

Q = [(k-1,i) for i,k in enumerate(Q)]
Q.sort()

P = [start]
for d in D:
  a = P[-1]
  b = abs(a-d)
  P.append(min(a,b))

result = [None]*len(Q)

x = 1

for i,d in zip(reversed(range(len(D))), reversed(D)):
  if Q[-1][0] == i:
    result[Q[-1][1]] = P[i] >= x
    Q.pop()
    if not Q:
      break

  if abs(x-d) < x:
    x += d

for r in result:
  print('YES' if r else 'NO')
Buggy python submission (verdict: Runtime Error):
from bisect import bisect_left
N = int(input())
A = list(int(input()) for _ in range(N))
S = sorted(S)
A = [bisect_left(S,a) for a in A]
print((sum((i-j)%2 for i,j in enumerate(A))+1)//2)

Fixed submission:
from bisect import bisect_left
N = int(input())
A = list(int(input()) for _ in range(N))
S = sorted(A)
A = [bisect_left(S,a) for a in A]
print((sum((i-j)%2 for i,j in enumerate(A))+1)//2)

Buggy python submission (verdict: Time Limit Exceeded):
from bisect import bisect_left
N = int(input())
A = list(int(input()) for _ in range(N))
A = [bisect_left(sorted(A),a) for a in A]
print((sum((i-j)%2 for i,j in enumerate(A))+1)//2)

Fixed submission:
from bisect import bisect_left
N = int(input())
A = list(int(input()) for _ in range(N))
S = sorted(A)
A = [bisect_left(S,a) for a in A]
print((sum((i-j)%2 for i,j in enumerate(A))+1)//2)
Buggy python submission (verdict: Time Limit Exceeded):
class SegmentTree():
    def __init__(self, n):
        self.n = n
        self.dp = [0 for _ in range(2 * n)]
        
    def update(self, k, a):
        k += self.n
        self.dp[k] = a
        while (k > 1):
            self.dp[k >> 1] = max(self.dp[k], self.dp[k ^ 1])
            k >>= 1
        
    def query(self, l, r):
        res = 0
        l, r = l + self.n, r + self.n
        while l < r:
            if l & 1:
                res = max(res, self.dp[l])
                l += 1
            if r & 1:
                res = max(res, self.dp[r - 1])
            l, r = l >> 1, r >> 1
        return res
    
    
n, k = map(int, input().split())
A = []
for _ in range(n):
    a = int(input())
    A.append(a)
num = max(A) + k + 1

st = SegmentTree(num)
for i in range(n):
    x = A[i]
    l = max(x - k, 0)
    r = min(x + k + 1, num)
    now = st.query(l, r) + 1
    st.update(x, now)
print(max(st.dp))    

Fixed submission:
class SegmentTree():
    def __init__(self, n):
        self.n = n
        self.dp = [0 for _ in range(2 * n)]
        
    def update(self, k, a):
        k += self.n
        self.dp[k] = a
        while (k > 1):
            self.dp[k >> 1] = max(self.dp[k], self.dp[k ^ 1])
            k >>= 1
        
    def query(self, l, r):
        res = 0
        l, r = l + self.n, r + self.n
        while l < r:
            if l & 1:
                res = max(res, self.dp[l])
                l += 1
            if r & 1:
                res = max(res, self.dp[r - 1])
            l, r = l >> 1, r >> 1
        return res
    
    
n, k = map(int, input().split())
A = []
for _ in range(n):
    a = int(input())
    A.append(a)
num = max(A) + k + 1

st = SegmentTree(num)
for i in range(n):
    x = A[i]
    l = max(x - k, 0)
    r = min(x + k + 1, num)
    now = st.query(l, r) + 1
    st.update(x, now)
print(max(st.dp))
Buggy python submission (verdict: Time Limit Exceeded):
def make_divisors(n):
    cnt = 0
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            cnt += 1
            if i != n // i:
                cnt += 1
    return cnt

n = int(input())
ans = 0
for i in range(1, n):
    ans += make_divisors(i)
print(ans)

Fixed submission:
ans = 0
n = int(input())
for i in range(1, n):
    ans += n // i
    if n % i == 0:
        ans -= 1
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
n, k = map(int, input().split())
value = []
for _ in range(k):
    l, r = map(int, input().split())
    value.append([l, r])
value.sort()
mod = 998244353
dp = [0 for _ in range(n)]
dp[0] = 1
for i in range(n - 1):
    for l, r in value:
        for dist in range(l, min(r + 1, n - 1 - i + 1)):
            if i + dist <= n - 1:
                dp[i + dist] = (dp[i] + dp[i + dist]) % mod
print(dp[-1])

Fixed submission:
mod = 998244353
n, k = map(int, input().split())
index_list = []
for _ in range(k):
    l, r = map(int, input().split())
    index_list.append([l, r])

dp = [0 for _ in range(n)]
dpsum = [0 for _ in range(n)]
dp[0] = 1
dpsum[0] = 1
for i in range(1, n):
    for l, r in index_list:
        l_index = max(i - r - 1, -1)
        r_index = i - l
        if r_index >= 0:
            dp[i] += dpsum[r_index]
            if (r_index > l_index) & (l_index >= 0):
                dp[i] -= dpsum[l_index]
    dp[i] = dp[i] % mod
    dpsum[i] = (dp[i] + dpsum[i - 1]) % mod
print(dp[-1])

Buggy python submission (verdict: Time Limit Exceeded):
n, k = map(int, input().split())
value = []
for _ in range(k):
    l, r = map(int, input().split())
    tmp = [i for i in range(l, r + 1)]
    value += tmp
value.sort()
mod = 998244353
dp = [0 for _ in range(n)]
dp[0] = 1
for i in range(n - 1):
    for dist in value:
        if i + dist <= n - 1:
            dp[i + dist] = (dp[i] + dp[i + dist]) % mod
print(dp[-1])

Fixed submission:
mod = 998244353
n, k = map(int, input().split())
index_list = []
for _ in range(k):
    l, r = map(int, input().split())
    index_list.append([l, r])

dp = [0 for _ in range(n)]
dpsum = [0 for _ in range(n)]
dp[0] = 1
dpsum[0] = 1
for i in range(1, n):
    for l, r in index_list:
        l_index = max(i - r - 1, -1)
        r_index = i - l
        if r_index >= 0:
            dp[i] += dpsum[r_index]
            if (r_index > l_index) & (l_index >= 0):
                dp[i] -= dpsum[l_index]
    dp[i] = dp[i] % mod
    dpsum[i] = (dp[i] + dpsum[i - 1]) % mod
print(dp[-1])

Buggy python submission (verdict: Time Limit Exceeded):
n, k = map(int, input().split())
value = []
for _ in range(k):
    l, r = map(int, input().split())
    tmp = [i for i in range(l, r + 1)]
    value += tmp
value.sort()
mod = 998244353
dp = [0 for _ in range(n)]
dp[0] = 1
for i in range(n - 1):
    for dist in value:
        if i + dist <= n - 1:
            dp[i + dist] = (dp[i] + dp[i + dist]) % mod
print(dp[-1])

Fixed submission:
mod = 998244353
n, k = map(int, input().split())
index_list = []
for _ in range(k):
    l, r = map(int, input().split())
    index_list.append([l, r])

dp = [0 for _ in range(n)]
dpsum = [0 for _ in range(n)]
dp[0] = 1
dpsum[0] = 1
for i in range(1, n):
    for l, r in index_list:
        l_index = max(i - r - 1, -1)
        r_index = i - l
        if r_index >= 0:
            dp[i] += dpsum[r_index]
            if (r_index > l_index) & (l_index >= 0):
                dp[i] -= dpsum[l_index]
    dp[i] = dp[i] % mod
    dpsum[i] = (dp[i] + dpsum[i - 1]) % mod
print(dp[-1])
Buggy python submission (verdict: Time Limit Exceeded):
n, x, m = map(int, input().split())
result = [x]
for i in range(m):
    y = (result[-1] * result[-1]) % m
    if y in result:
        start = result.index(y)
        end = i
        break
    result.append(y)

loop = result[start:]
a = start
b = (n - a) // len(loop)
c = (n - a) % len(loop)
ans = sum(result[:a]) + (sum(loop) * b) + sum(loop[:c])
print(ans)

Fixed submission:
n, x, m = map(int, input().split())
result = [x]
for i in range(m):
    y = (result[-1] * result[-1]) % m
    if y in result:
        start = result.index(y)
        end = i
        break
    result.append(y)

loop = result[start:]
a = start
b = (n - a) // len(loop)
c = (n - a) % len(loop)
ans = sum(result[:a]) + (sum(loop) * b) + sum(loop[:c])
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n, x, m = map(int, input().split())

doubling_list = [x % m]
t = n * (n - 1) // 2
bit = t.bit_length()
for i in range(n - 1):
    doubling = pow(doubling_list[-1], 2, m)
    doubling_list.append(doubling)


print(sum(doubling_list))

Fixed submission:
n, x, m = map(int, input().split())
result = [x]
for i in range(m):
    y = (result[-1] * result[-1]) % m
    if y in result:
        start = result.index(y)
        end = i
        break
    result.append(y)

loop = result[start:]
a = start
b = (n - a) // len(loop)
c = (n - a) % len(loop)
ans = sum(result[:a]) + (sum(loop) * b) + sum(loop[:c])
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
c = list(input())
color = [i == 'R' for i in c]
ans = 0
checked = 0
for right in range(N - 1, -1, -1):
    if color[right]:
        for left in range(checked, right):            
            if not color[left]:
                color[right], color[left] = color[left], color[right]
                ans += 1
                checked = left
                break
print(ans)                

Fixed submission:
N = int(input())
c = list(input())
color = [i == 'R' for i in c]
ans = 0
checked = 0
for right in range(N - 1, -1, -1):
    if color[right]:
        for left in range(checked, right):            
            if not color[left]:
                color[right], color[left] = color[left], color[right]
                ans += 1
                checked = left
                break
        checked = left
print(ans)
Buggy python submission (verdict: Runtime Error):
import copy
import math

def calc(x, aa):
    a = copy.deepcopy(aa)
    cnt = k
    while cnt > 0:
        for i in range(n):
            if (a[i] > x):
                cnt -= a[i] // x
                a[i] = x
            if cnt < 0:
                return False
        if cnt < 0:
            return False
        return True


n, k = map(int, input().split())
a = [int(i) for i in input().split()]
left, right = 0, max(a)
while (right - left) > 1:
    middle = (left + right) / 2
    if calc(middle, a):
        right = middle
    else:
        left = middle
print(math.ceil(middle))



Fixed submission:
import copy
import math

def calc(x, aa):
    a = copy.deepcopy(aa)
    cnt = k
    while cnt > 0:
        for i in range(n):
            if (a[i] > x):
                cnt -= a[i] // x
                a[i] = x
            if cnt < 0:
                return False
        if cnt < 0:
            return False
        return True
    
n, k = map(int, input().split())
a = [int(i) for i in input().split()]
left, right = 0, max(a)
if k == 0:
    print(right)
    exit()
    
while (right - left) > 1:
    middle = (left + right) / 2
    if calc(middle, a):
        right = middle
    else:
        left = middle        
for i in range(max(math.floor(left) - 10, 1), math.ceil(right) + 10):
    if calc(i, a):
        print(i)
        exit()

Buggy python submission (verdict: Runtime Error):
import copy
import math
n, k = map(int, input().split())
a = [int(i) for i in input().split()]
left, right = 0, max(a)
while (right - left) > 1:
    middle = (left + right) / 2
    if calc(middle, a):
        right = middle
    else:
        left = middle
print(math.ceil(middle))

def calc(x, aa):
    a = copy.deepcopy(aa)
    cnt = k
    while cnt > 0:
        for i in range(n):
            if (a[i] > x):
                cnt -= a[i] // x
                a[i] = x
            if cnt < 0:
                return False
        if cnt < 0:
            return False
        return True

Fixed submission:
import copy
import math

def calc(x, aa):
    a = copy.deepcopy(aa)
    cnt = k
    while cnt > 0:
        for i in range(n):
            if (a[i] > x):
                cnt -= a[i] // x
                a[i] = x
            if cnt < 0:
                return False
        if cnt < 0:
            return False
        return True
    
n, k = map(int, input().split())
a = [int(i) for i in input().split()]
left, right = 0, max(a)
if k == 0:
    print(right)
    exit()
    
while (right - left) > 1:
    middle = (left + right) / 2
    if calc(middle, a):
        right = middle
    else:
        left = middle        
for i in range(max(math.floor(left) - 10, 1), math.ceil(right) + 10):
    if calc(i, a):
        print(i)
        exit()
Buggy python submission (verdict: Time Limit Exceeded):
def bit_add(arr, n, i, x):
    while i <= n:
        arr[i] += x
        i += i & -i

def bit_sum(arr, i):
    result = 0
    while i > 0:
        result += arr[i]
        i ^= i & -i
    return result

n, q = map(int, input().split())
color = [int(i) for i in input().split()]
query = []
rli = [0] * q
for i in range(q):
    l, r = map(int, input().split())
    query.append((l, r))
    rli[i] = (r << 40) + (l << 20) + i
rli.sort()
mask = (1 << 20) - 1
bn = n + 2
bit = [0] * (bn + 1)
ans = [0] * q
appeared = [-1] * (n + 1)
p = 0

for j in range(q):
    k = rli[j]
    r = k >> 40
    l = (k >> 20) & mask
    i = k & mask
    while p < r:
        c = color[p]
        before_appeared = appeared[c]
        if before_appeared != -1:
            bit_add(bit, bn, before_appeared + 2, -1)
        bit_add(bit, bn, p + 2, 1)
        appeared[c] = p
        p += 1
    ans[i] = bit_sum(bit, r + 1) - bit_sum(bit, l)
print('\n'.join(map(str, ans)))    

Fixed submission:
def bit_add(arr, n, i, x):
    while i <= n:
        arr[i] += x
        i += i & -i

def bit_sum(arr, i):
    result = 0
    while i > 0:
        result += arr[i]
        i ^= i & -i
    return result

n, q = map(int, input().split())
color = [int(i) for i in input().split()]
query = []
rli = [0] * q
for i in range(q):
    l, r = map(int, input().split())
    query.append((l, r))
    rli[i] = (r << 40) + (l << 20) + i
rli.sort()
mask = (1 << 20) - 1
bn = n + 2
bit = [0] * (bn + 1)
ans = [0] * q
appeared = [-1] * (n + 1)
p = 0

for j in range(q):
    k = rli[j]
    r = k >> 40
    l = (k >> 20) & mask
    i = k & mask
    while p < r:
        c = color[p]
        before_appeared = appeared[c]
        if before_appeared != -1:
            bit_add(bit, bn, before_appeared + 2, -1)
        bit_add(bit, bn, p + 2, 1)
        appeared[c] = p
        p += 1
    ans[i] = bit_sum(bit, r + 1) - bit_sum(bit, l)
print('\n'.join(map(str, ans)))
Buggy python submission (verdict: Runtime Error):
mod = 10 ** 9 + 7
N, K = map(int, input().split())
A = [int(i) for i in input().split()]
A_minus = []
A_plus = []
for i in A:
    if i < 0:
        A_minus.append(i)
    elif i >= 0:
        A_plus.append(i)

len_plus = len(A_plus)
len_minus = len(A_minus)
plus = True
if (K > len_plus):
    if K % 2 == 1:
        if len_plus > 0:
                len_plus -= 1
                K -= 1
    K -= (len_plus // 2) * 2
    if K < len_minus:
        plus = False
ans = 1
if plus:
    A_plus.sort(reverse=True)
    A_minus.sort()
    for i in range(K):
        if ans < 0:
            ans *= A_minus.pop()
            continue
        if i < K - 1:
            if (len(A_plus) > 0) & (len(A_minus) > 0):
                if A_plus[0] < abs(A_minus[0]):
                    ans *= A_minus.pop()
                    continue
            if len(A_plus) > 0:
                ans *= A_plus.pop()
else:
    A_plus.sort()
    A_minus.sort(reverse=True)
    for i in range(K):
        if ans < 0:
            ans *= A_minus.pop()
            continue
        if i < K - 1:
            if (len(A_plus) > 0) & (len(A_minus) > 0):
                if A_plus[0] < abs(A_minus[0]):
                    ans *= A_minus.pop()
                    continue
            if len(A_plus) > 0:
                ans *= A_plus.pop()
print(ans % mod)

Fixed submission:
def solve():
    can_positive = False
    if len(P) > 0:
        if k < n:
            can_positive = True
        else:
            can_positive = len(M) % 2 == 0
    else:
        can_positive = k % 2 == 0
    if not can_positive:
        return sorted(P + M, key=lambda x: abs(x))[:k]
    P.sort()
    M.sort(reverse=True)
    a = [P.pop()] if k % 2 else [1]
    while len(P) >= 2:
        a.append(P.pop() * P.pop())
    while len(M) >= 2:
        a.append(M.pop() * M.pop())
    return a[:1] + sorted(a[1:], reverse=True)[:(k - k%2) // 2]

n, k = map(int, input().split())
P, M = [], []
for a in map(int, input().split()):
    if a < 0:
        M.append(a)
    else:
        P.append(a)
ans, mod = 1, 10 ** 9 + 7
for a in solve():
    ans *= a
    ans %= mod
print(ans)
Buggy python submission (verdict: Runtime Error):
D = int(input())
c_list = [int(i) for i in input().split()]
s_list = []
t_list = []
for _ in range(D):
    s = [int(i) for i in input().split()]
    s_list.append(s)
for _ in range(D):
    t_list.append(int(input()))

t_list[d] = q
happy = 0
recent = [0 for _ in range(26)]
for _ in range(D):
    print(1)

Fixed submission:
D = int(input())
c_list = [int(i) for i in input().split()]
s_list = []
t_list = []
for _ in range(D):
    s = [int(i) for i in input().split()]
    s_list.append(s)
happy = 0
recent = [0 for _ in range(26)]
for _ in range(D):
    print(1)
Buggy python submission (verdict: Time Limit Exceeded):
a, b, c, d = map(int, input().split())
dp = [[1 for _ in range(d - b + 1)] for _ in range(c - a + 1)]

mod = 998244353
for x in range(c - a + 1):
    for y in range(d - b + 1):
        if x == 0:
            if y > 0:
                dp[x][y] = dp[x][y - 1] * (a + x) % mod
        elif y == 0:
            dp[x][y] = dp[x - 1][y] * (b + y) % mod
        else:
            dp[x][y] = (((dp[x - 1][y] * (b + y)) + (dp[x][y - 1] * (a + x))) - (dp[x - 1][y - 1] * (a + x - 1) * (b + y - 1))) % mod

print(dp[-1][-1])

Fixed submission:
a, b, c, d = map(int, input().split())
dp = [[1 for _ in range(d - b + 1)] for _ in range(c - a + 1)]

mod = 998244353
for x in range(c - a + 1):
    for y in range(d - b + 1):
        if x == 0:
            if y > 0:
                dp[x][y] = dp[x][y - 1] * (a + x) % mod
        elif y == 0:
            dp[x][y] = dp[x - 1][y] * (b + y) % mod
        else:
            dp[x][y] = (((dp[x - 1][y] * (b + y)) + (dp[x][y - 1] * (a + x))) - (dp[x - 1][y - 1] * (a + x - 1) * (b + y - 1))) % mod

print(dp[-1][-1])
Buggy python submission (verdict: Time Limit Exceeded):
n, k = map(int, input().split())
a = [int(i) for i in input().split()]
for j in range(min(k, 42)):
    bit = [0 for _ in range(n)]
    for i in range(n):
        bit[max(i - a[i], 0)] += 1
        if i + a[i] + 1 < n:
            bit[i + a[i] + 1] -= 1
    result = [bit[0]]
    for i in range(n - 1):
        result.append(bit[i + 1] + result[-1])
    a = result
print(' '.join(list(map(str, a))))

Fixed submission:
n, k = map(int, input().split())
a = [int(i) for i in input().split()]
for j in range(min(k, 42)):
    bit = [0 for _ in range(n)]
    for i in range(n):
        bit[max(i - a[i], 0)] += 1
        if i + a[i] + 1 < n:
            bit[i + a[i] + 1] -= 1
    result = [bit[0]]
    for i in range(n - 1):
        result.append(bit[i + 1] + result[-1])
    a = result
print(' '.join(list(map(str, a))))
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
n_list = [int(i) for i in input().split()]
ans = 1
n_list.sort()
for i in n_list:
    ans *= i
    if ans > 10 ** 18:
        ans = -1
print(ans)


Fixed submission:
n = int(input())
n_list = [int(i) for i in input().split()]
ans = 1
n_list.sort()
n_list = list(reversed(n_list))
if 0 in n_list:
    print(0)
else:
    for i in n_list:
        ans *= i
        if ans > 10 ** 18:
            ans = -1
            break
    print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
n_list = [int(i) for i in input().split()]
ans = 1
n_list.sort()
for i in n_list:
    ans *= i
    if ans > 10 ** 18:
        ans = -1
print(ans)

Fixed submission:
n = int(input())
n_list = [int(i) for i in input().split()]
ans = 1
n_list.sort()
n_list = list(reversed(n_list))
if 0 in n_list:
    print(0)
else:
    for i in n_list:
        ans *= i
        if ans > 10 ** 18:
            ans = -1
            break
    print(ans)
Buggy python submission (verdict: Runtime Error):
s = input()
ans = ''
for i in range(len(s)):
  s_i = s[i]
  if s_i = '?':
    if i < len(s):
      if s[i + 1] == 'D':
        ans += 'P'
        continue
    else:
      ans += 'D'
      continue
  ans += s_i
print(ans)
      

Fixed submission:
s = input()
ans = ''
for i in range(len(s)):
  s_i = s[i]
  if s_i == '?':
      ans += 'D'
      continue
  ans += s_i
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
n, m = map(int, input().split())
graph = [[] for _ in range(n)]
for _ in range(m):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    graph[a].append(b)
    graph[b].append(a)
    
q = deque()
q.append(0)

ans = [-1 for _ in range(n)]
distance = [float('inf') for _ in range(n)]
distance[0] = 0
ans[0] = 0

visited = set({})

while q:
    start = q.popleft()
    visited.add(start)
    for i in graph[start]:
        distance[i] = min(distance[i], distance[start] + 1)
        if i not in visited:
            q.append(i)
            if (ans[i] == -1):
                ans[i] = start
                
if -1 not in ans:
    print('Yes')
    for i in range(1, n):
        print(ans[i] + 1)
else:
    print('No')


Fixed submission:
from collections import deque
n, m = map(int, input().split())
graph = [[] for _ in range(n)]
for _ in range(m):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    graph[a].append(b)
    graph[b].append(a)
    
q = deque()
q.append(0)

ans = [-1 for _ in range(n)]
ans[0] = 0

visited = set({})

while q:
    start = q.popleft()
    visited.add(start)
    for i in graph[start]:
        if ans[i] == -1:
            q.append(i)
            ans[i] = start
                
if -1 not in ans:
    print('Yes')
    for i in range(1, n):
        print(ans[i] + 1)
else:
    print('No')

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
n, m = map(int, input().split())
graph = [[] for _ in range(n)]
for _ in range(m):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    graph[a].append(b)
    graph[b].append(a)
    
q = deque()
q.append(0)

ans = [-1 for _ in range(n)]
distance = [float('inf') for _ in range(n)]
distance[0] = 0
ans[0] = 0

visited = set({})

while q:
    start = q.popleft()
    visited.add(start)
    for i in graph[start]:
        distance[i] = min(distance[i], distance[start] + 1)
        if i not in visited:
            q.append(i)
            if (ans[i] == -1):
                ans[i] = start
                
if -1 not in ans:
    print('Yes')
    for i in range(1, n):
        print(ans[i] + 1)
else:
    print('No')

Fixed submission:
from collections import deque
n, m = map(int, input().split())
graph = [[] for _ in range(n)]
for _ in range(m):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    graph[a].append(b)
    graph[b].append(a)
    
q = deque()
q.append(0)

ans = [-1 for _ in range(n)]
ans[0] = 0

visited = set({})

while q:
    start = q.popleft()
    visited.add(start)
    for i in graph[start]:
        if ans[i] == -1:
            q.append(i)
            ans[i] = start
                
if -1 not in ans:
    print('Yes')
    for i in range(1, n):
        print(ans[i] + 1)
else:
    print('No')