Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
import sys

def main():
    input = sys.stdin.readline
    N, M = map(int, input().split())
    edges = [[] for _ in range(N)]
    for _ in range(M):
        a, b = map(int, input().split())
        a -= 1
        b -= 1
        edges[a].append(b)
        edges[b].append(a)

    visited = [False] * N
    rooms = [-1] * N
    def bfs(u):
        q = deque()
        q.append((u, -1))
        while 0 < len(q):
            (v, p) = q.popleft()
            visited[v] = True
            if rooms[v] == -1 and p != -1:
                rooms[v] = p
            for e in edges[v]:
                if visited[e]:
                    continue
                q.append((e, v))

    bfs(0)
    print("Yes")
    for x in rooms[1:]:
        print(x + 1)

if __name__ == '__main__':
    main()

Fixed submission:
from collections import deque

N, M = map(int, input().split())
edges = [[] for _ in range(N)]
for _ in range(M):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    edges[a].append(b)
    edges[b].append(a)

rooms = [-1] * N
def bfs(u):
    q = deque()
    q.append(u)
    while 0 < len(q):
        v = q.popleft()
        for e in edges[v]:
            if rooms[e] == -1:
                rooms[e] = v + 1
                q.append(e)

bfs(0)
print("Yes")
print(*rooms[1:], sep="\n")

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
import sys

def main():
    input = sys.stdin.readline
    N, M = map(int, input().split())
    edges = [[] for _ in range(N)]
    for _ in range(M):
        a, b = map(int, input().split())
        a -= 1
        b -= 1
        edges[a].append(b)
        edges[b].append(a)

    visited = [False] * N
    rooms = [-1] * N
    def bfs(u):
        q = deque()
        q.append((u, -1))
        while 0 < len(q):
            (v, p) = q.popleft()
            visited[v] = True
            if rooms[v] == -1 and p != -1:
                rooms[v] = p
            for e in edges[v]:
                if visited[e]:
                    continue
                q.append((e, v))

    bfs(0)
    print("Yes")
    for x in rooms[1:]:
        print(x + 1)

if __name__ == '__main__':
    main()

Fixed submission:
from collections import deque

N, M = map(int, input().split())
edges = [[] for _ in range(N)]
for _ in range(M):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    edges[a].append(b)
    edges[b].append(a)

rooms = [-1] * N
def bfs(u):
    q = deque()
    q.append(u)
    while 0 < len(q):
        v = q.popleft()
        for e in edges[v]:
            if rooms[e] == -1:
                rooms[e] = v + 1
                q.append(e)

bfs(0)
print("Yes")
print(*rooms[1:], sep="\n")

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

visited = [False] * N
rooms = [-1] * N
def bfs(u):
    q = deque()
    q.append((u, -1))
    while 0 < len(q):
        (v, p) = q.popleft()
        visited[v] = True
        if p != -1 and rooms[v] == -1:
            rooms[v] = p
        for e in edges[v]:
            if visited[e]:
                continue
            q.append((e, v))

bfs(0)
print("Yes")
for x in rooms[1:]:
    print(x + 1)

Fixed submission:
from collections import deque

N, M = map(int, input().split())
edges = [[] for _ in range(N)]
for _ in range(M):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    edges[a].append(b)
    edges[b].append(a)

rooms = [-1] * N
def bfs(u):
    q = deque()
    q.append(u)
    while 0 < len(q):
        v = q.popleft()
        for e in edges[v]:
            if rooms[e] == -1:
                rooms[e] = v + 1
                q.append(e)

bfs(0)
print("Yes")
print(*rooms[1:], sep="\n")

Buggy python submission (verdict: Time Limit Exceeded):
import queue
N, M = map(int, input().split())
edges = [[] for _ in range(N)]
for _ in range(M):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    edges[a].append(b)
    edges[b].append(a)

visited = [False] * N
rooms = [-1] * N
def bfs(u):
    q = queue.Queue()
    q.put((u, -1))
    while not q.empty():
        (v, p) = q.get()
        visited[v] = True
        if p != -1 and rooms[v] == -1:
            rooms[v] = p
        for e in edges[v]:
            if visited[e]:
                continue
            q.put((e, v))

bfs(0)
print("Yes")
for x in rooms[1:]:
    print(x + 1)

Fixed submission:
from collections import deque

N, M = map(int, input().split())
edges = [[] for _ in range(N)]
for _ in range(M):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    edges[a].append(b)
    edges[b].append(a)

rooms = [-1] * N
def bfs(u):
    q = deque()
    q.append(u)
    while 0 < len(q):
        v = q.popleft()
        for e in edges[v]:
            if rooms[e] == -1:
                rooms[e] = v + 1
                q.append(e)

bfs(0)
print("Yes")
print(*rooms[1:], sep="\n")

Buggy python submission (verdict: Time Limit Exceeded):
import queue
N, M = map(int, input().split())
edges = [[] for _ in range(N)]
for _ in range(M):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    edges[a].append(b)
    edges[b].append(a)

visited = [False] * N
dist = [float('inf')] * N
rooms = [-1] * N
def bfs(u):
    q = queue.Queue()
    q.put((u, 0, -1))
    while not q.empty():
        (v, d, p) = q.get()
        visited[v] = True
        if dist[v] > d:
            dist[v] = d
            if p != -1:
                rooms[v] = p
        for e in edges[v]:
            if visited[e]:
                continue
            q.put((e, d + 1, v))

dist[0] = 0
bfs(0)
print("Yes")
for x in rooms[1:]:
    print(x + 1)

Fixed submission:
from collections import deque

N, M = map(int, input().split())
edges = [[] for _ in range(N)]
for _ in range(M):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    edges[a].append(b)
    edges[b].append(a)

rooms = [-1] * N
def bfs(u):
    q = deque()
    q.append(u)
    while 0 < len(q):
        v = q.popleft()
        for e in edges[v]:
            if rooms[e] == -1:
                rooms[e] = v + 1
                q.append(e)

bfs(0)
print("Yes")
print(*rooms[1:], sep="\n")