Buggy python submission (verdict: Time Limit Exceeded):
n, m = map(int, input().split())
numlist = []
record = [0] * (n-1)
route = []
flag = 0
for i in range(m):
    temp = list(map(int, input().split()))
    if(temp[0] == 1):
        m -= 1
        record[temp[1] - 2] = 1
        flag += 1
        route.append(temp[1])
    elif(temp[1] == 1):
        m -= 1
        record[temp[0] - 2] = 1
        flag += 1
        route.append(temp[0])
    else:
        numlist += temp
newlist = []
while(flag < n-1):
    newroute = []
    for i in range(len(route)):
        for j in range(m):
            if(numlist[j * 2] == route[i]):
                m -= 1
                if(record[numlist[j * 2 + 1] - 2] == 0):
                    record[numlist[j * 2 + 1] - 2] = route[i]
                    flag += 1
                newroute.append(numlist[j * 2 + 1])
            elif(numlist[j * 2 + 1] == route[i]):
                m -= 1
                if(record[numlist[j * 2] - 2] == 0):
                    record[numlist[j * 2] - 2] = route[i]
                    flag += 1
                newroute.append(numlist[j * 2])
            else:
                newlist += numlist[j*2:j*2 + 2]
        numlist = newlist
        newlist = []
    route = newroute
print("Yes")
for i in record:
    print(i)

Fixed submission:
import queue
n, m = map(int, input().split())
graph = [[] for i in range(n)]
depth = [-1] * n
route = []
result = [0] * (n - 1)
for i in range(m):
    temp = list(map(int, input().split()))
    graph[temp[0] - 1].append(temp[1] - 1)
    graph[temp[1] - 1].append(temp[0] - 1)
    route.append(temp)
q = queue.Queue()
q.put(0)
depth[0] = 0
for i in range(n-1):
    item = q.get()
    for j in graph[item]:
        if(depth[j] == -1):
            q.put(j)
            depth[j] = depth[item] + 1
print("Yes")
for i in route:
    if(i[0] == 1):
        result[i[1] - 2] = 1
    elif(i[1] == 1):
        result[i[0] - 2] = 1
    elif(depth[i[0] - 1] - depth[i[1] - 1] == 1):
        result[i[0] - 2] = i[1]
    elif(depth[i[1] - 1] - depth[i[0] - 1] == 1):
        result[i[1] - 2] = i[0]
for i in result:
    print(i)

Buggy python submission (verdict: Time Limit Exceeded):
n, m = map(int, input().split())
numlist = []
record = [0] * (n-1)
route = []
for i in range(m):
    temp = list(map(int, input().split()))
    if(temp[0] == 1):
        m -= 1
        record[temp[1] - 2] = 1
        route.append(temp[1])
    elif(temp[1] == 1):
        m -= 1
        record[temp[0] - 2] = 1
        route.append(temp[0])
    else:
        numlist += temp
newlist = []
flag = 1
for i in record:
    flag *= i 
while(flag == 0):
    newroute = []
    for i in range(len(route)):
        for j in range(m):
            if(numlist[j * 2] == route[i]):
                m -= 1
                if(record[numlist[j * 2 + 1] - 2] == 0):
                    record[numlist[j * 2 + 1] - 2] = route[i]
                newroute.append(numlist[j * 2 + 1])
            elif(numlist[j * 2 + 1] == route[i]):
                m -= 1
                if(record[numlist[j * 2] - 2] == 0):
                    record[numlist[j * 2] - 2] = route[i]
                newroute.append(numlist[j * 2])
            else:
                newlist += numlist[j*2:j*2 + 2]
        numlist = newlist
        newlist = []
    route = newroute
    flag = 1
    for i in record:
        flag *= i
print("Yes")
for i in record:
    print(i)

Fixed submission:
import queue
n, m = map(int, input().split())
graph = [[] for i in range(n)]
depth = [-1] * n
route = []
result = [0] * (n - 1)
for i in range(m):
    temp = list(map(int, input().split()))
    graph[temp[0] - 1].append(temp[1] - 1)
    graph[temp[1] - 1].append(temp[0] - 1)
    route.append(temp)
q = queue.Queue()
q.put(0)
depth[0] = 0
for i in range(n-1):
    item = q.get()
    for j in graph[item]:
        if(depth[j] == -1):
            q.put(j)
            depth[j] = depth[item] + 1
print("Yes")
for i in route:
    if(i[0] == 1):
        result[i[1] - 2] = 1
    elif(i[1] == 1):
        result[i[0] - 2] = 1
    elif(depth[i[0] - 1] - depth[i[1] - 1] == 1):
        result[i[0] - 2] = i[1]
    elif(depth[i[1] - 1] - depth[i[0] - 1] == 1):
        result[i[1] - 2] = i[0]
for i in result:
    print(i)