Problem p02678 (python)

Fixed submission:

import sys, os, re
from pprint import pprint
from math import sin, cos, sqrt, radians

n, m = list(map(int, sys.stdin.readline().strip().split()))
g = [[] for x in range(n)]

for i in range(m):
    x, y = map(int, sys.stdin.readline().strip().split())
    x -= 1
    y -= 1
    g[x].append(y)
    g[y].append(x)

if [] in g:
    print("No")
    exit(0)

ans = {0: -1}
tmp = [0]

while len(ans) < n:
    tmp_next = []
    for k in tmp:
        for node in g[k]:
            if node not in ans:
                ans[node] = k
                tmp_next.append(node) 
                flag = True

        tmp = tmp_next
        if flag == False:
            break

    """
    flag = False    
    for k, v in list(ans.items()):
        for node in g[k]:
            if node not in ans:
                ans[node] = k
                flag = True
    if flag == False:
        break
    """

print("Yes")
for i in range(1, n):
    print(ans[i]+1)

Buggy submission (verdict: Runtime Error):
import sys, os, re
from pprint import pprint
from math import sin, cos, sqrt, radians

n, m = list(map(int, sys.stdin.readline().strip().split()))
g = [[] for x in range(n)]

for i in range(n):
    x, y = map(int, sys.stdin.readline().strip().split())
    x -= 1
    y -= 1
    g[x].append(y)
    g[y].append(x)

ans = {0: -1}

while len(ans) < n:
    flag = False
    for k, v in ans.items():
        for node in g[k]:
            if node not in ans:
                ans[node] = k
                flag = True
    if flag == False:
        break

print("Yes")
for i in range(1, n):
    print(ans[i]+1)

Buggy submission (verdict: Runtime Error):

import sys, os, re
from pprint import pprint
from math import sin, cos, sqrt, radians

n, m = list(map(int, sys.stdin.readline().strip().split()))
g = [[] for x in range(n)]

for i in range(n):
    x, y = list(map(int, sys.stdin.readline().strip().split()))
    x -= 1
    y -= 1

ans = {0: -1}

while len(ans) < n:
    flag = False
    for k, v in ans.items():
        for node in g[k]:
            if node not in ans:
                ans[node] = k
                flag = True
    if flag == False:
        break

print("Yes")

Buggy submission (verdict: Runtime Error):
import sys, os, re
from pprint import pprint
from math import sin, cos, sqrt, radians

n, m = list(map(int, sys.stdin.readline().strip().split()))
g = [[] for x in range(n)]

for i in range(n):
    x, y = list(map(int, sys.stdin.readline().strip().split()))
    x -= 1
    y -= 1
    g[x].append(y)
    g[y].append(x)

ans = {0: -1}

print("Yes")

Buggy submission (verdict: Runtime Error):
import sys, os, re
from pprint import pprint
from math import sin, cos, sqrt, radians

n, m = list(map(int, sys.stdin.readline().strip().split()))
g = [[] for x in range(n)]

for i in range(n):
    x, y = list(map(int, sys.stdin.readline().strip().split()))
    x -= 1
    y -= 1
ans = {0: -1}

while len(ans) < n:
    flag = False
    for k, v in ans.items():
        for node in g[k]:
            if node not in ans:
                ans[node] = k
                flag = True
    if flag == False:
        break

print("Yes")
for i in range(1, n):
    print(ans[i]+1)

Buggy submission (verdict: Runtime Error):
import sys, os, re
from pprint import pprint
from math import sin, cos, sqrt, radians

n, m = list(map(int, sys.stdin.readline().strip().split()))
g = [[] for x in range(n)]

for i in range(n):
    x, y = list(map(int, sys.stdin.readline().strip().split()))
    x -= 1
    y -= 1

ans = {0: -1}

while len(ans) < n:
    flag = False
    for k, v in ans.items():
        for node in g[k]:
            if node not in ans:
                ans[node] = k
                flag = True
    if flag == False:
        break

print("Yes")
for i in range(1, n):
    print(ans[i]+1)

Buggy submission (verdict: Runtime Error):

import sys, os, re
from pprint import pprint
from math import sin, cos, sqrt, radians

n, m = list(map(int, sys.stdin.readline().strip().split()))
g = [[] for x in range(n)]

for i in range(n):
    x, y = list(map(int, sys.stdin.readline().strip().split()))
    x -= 1
    y -= 1
    g[x].append(y)
    g[y].append(x)

ans = {0: -1}

print("Yes")
for i in range(1, n):
    print(ans[i]+1)

Buggy submission (verdict: Runtime Error):
import sys, os, re
from pprint import pprint
from math import sin, cos, sqrt, radians

n, m = list(map(int, sys.stdin.readline().strip().split()))
g = [[] for x in range(n)]

for i in range(n):
    x, y = list(map(int, sys.stdin.readline().strip().split()))
    x -= 1
    y -= 1
    g[x].append(y)
    g[y].append(x)

ans = {0: -1}

while len(ans) < n:
    flag = False
    for k, v in ans.items():
        for node in g[k]:
            if node not in ans:
                ans[node] = k
                flag = True
    if flag == False:
        break

print("Yes")
for i in range(1, n):
    print(ans[i]+1)

Buggy submission (verdict: Runtime Error):

import sys, os, re
from pprint import pprint
from math import sin, cos, sqrt, radians

n, m = list(map(int, sys.stdin.readline().strip().split()))
g = [[] for x in range(n)]

for i in range(n):
    x, y = list(map(int, sys.stdin.readline().strip().split()))
    x -= 1
    y -= 1
    g[x].append(y)
    g[y].append(x)

ans = {0: -1}

while len(ans) < n:
    flag = False
    for k, v in ans.items():
        for node in g[k]:
            if node not in ans:
                ans[node] = k
                flag = True
    if flag == False:
        break

print("Yes")
for i in range(1, n):
    print(ans[i]+1)