Buggy python submission (verdict: Time Limit Exceeded):
def main():
    n,m=map(int,input().split())
    ab = {}
    ab1 = {}
    for _ in range(m):
        a,b=map(int,input().split())
        ab.setdefault(a,[]).append(b)
        ab.setdefault(b,[]).append(a)
        ab1.setdefault(a,[]).append(b)
        ab1.setdefault(b,[]).append(a)
    g = []
    g.append(set([1]))
    marked = set()
    marked.add(1)
    while ab:
        g_add = set()
        for k in g[-1]:
            for x in ab[k]:
                if x not in marked:
                    g_add.add(x)
                    marked.add(x)
            del ab[k]
        g.append(g_add)
    if len(marked) < n:
        print('No')
        return
    print('Yes')
    result = [None] * n
    for i,s in enumerate(g):
        for k in s:
            result[k-1] = [e for e in g[i-1] if e in ab1[k]]
    for i in range(1,n):
        print(result[i][0])

main()

Fixed submission:
def main():
    n,m=map(int,input().split())
    ab = {}
    ab1 = {}
    for _ in range(m):
        a,b=map(int,input().split())
        ab.setdefault(a,set()).add(b)
        ab.setdefault(b,set()).add(a)
        ab1.setdefault(a,set()).add(b)
        ab1.setdefault(b,set()).add(a)
    g = []
    g.append(set([1]))
    marked = set()
    marked.add(1)
    while ab:
        g_add = set()
        for k in g[-1]:
            for x in ab[k]:
                if x not in marked:
                    g_add.add(x)
                    marked.add(x)
            del ab[k]
        g.append(g_add)
    if len(marked) < n:
        print('No')
        return
    print('Yes')
    result = [None] * n
    for i,s in enumerate(g):
        if not i:
            continue
        for k in s:
            result[k-1] = (g[i-1]&ab1[k]).pop()
    for i in range(1,n):
        print(result[i])

main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    n,m=map(int,input().split())
    ab = {}
    for _ in range(m):
        a,b=map(int,input().split())
        ab.setdefault(a,[]).append(b)
        ab.setdefault(b,[]).append(a)
    result = [None] * n
    result[0] = (-1, 0)
    d = 0
    while ab:
        for i,t in enumerate(result):
            if t and t[1] == d:
                if i+1 in ab:
                    for k in ab[i+1]:
                        if not result[k-1]:
                            result[k-1] = (i+1,d+1)
                    del ab[i+1]
        d+=1
    if None in result:
        print('No')
        return
    print('Yes')
    for i in range(1,n):
        print(result[i][0])

main()

Fixed submission:
def main():
    n,m=map(int,input().split())
    ab = {}
    ab1 = {}
    for _ in range(m):
        a,b=map(int,input().split())
        ab.setdefault(a,set()).add(b)
        ab.setdefault(b,set()).add(a)
        ab1.setdefault(a,set()).add(b)
        ab1.setdefault(b,set()).add(a)
    g = []
    g.append(set([1]))
    marked = set()
    marked.add(1)
    while ab:
        g_add = set()
        for k in g[-1]:
            for x in ab[k]:
                if x not in marked:
                    g_add.add(x)
                    marked.add(x)
            del ab[k]
        g.append(g_add)
    if len(marked) < n:
        print('No')
        return
    print('Yes')
    result = [None] * n
    for i,s in enumerate(g):
        if not i:
            continue
        for k in s:
            result[k-1] = (g[i-1]&ab1[k]).pop()
    for i in range(1,n):
        print(result[i])

main()