Problem p02761 (python)

Fixed submission:
from collections import defaultdict
N, M = map(int, input().split())  # ちょうどN桁, M個のクエリ
correspondence = defaultdict(set)
for _ in range(M):
    s, c = map(int, input().split())
    correspondence[s].add(c)
# print(correspondence)
if M == 0:
    if N == 1:
        print(0)
        exit()
    print("1"+"0"*(N-1))
    exit()
for k, v in correspondence.items():  # 要素が複数あったら-1を出力
    if len(v) > 1:
        print(-1)
        exit()
ans = ""
for i in range(1, N+1):
    if i in correspondence.keys():  # 指定があるならばそれにする
        ans += str(list(correspondence[i])[0])
    else:
        ans += "0"  # ないならば0にする
if ans[0] == "0":
    ans = list(ans)
    ans[0] = "1"
    ans = "".join(ans)

if N == 1 and 1 in correspondence.keys():
    print(list(correspondence[1])[0])
    exit()

if 1 in correspondence.keys() and list(correspondence[1])[0] == 0:  # 1桁目が0に指定されているなら終わり
    print(-1)
else:
    print(ans)

Buggy submission (verdict: Runtime Error):
from collections import defaultdict
N, M = map(int, input().split())  # ちょうどN桁, M個のクエリ
correspondence = defaultdict(set)
for _ in range(M):
    s, c = map(int, input().split())
    correspondence[s].add(c)
# print(correspondence)
for k, v in correspondence.items():  # 要素が複数あったら-1を出力
    if len(v) > 1:
        print(-1)
        exit()
if max(correspondence.keys()) > N:
    print(-1)
    exit()
ans = ""
for i in range(1, N+1):
    if i in correspondence.keys():  # 指定があるならばそれにする
        ans += str(list(correspondence[i])[0])
    else:
        ans += "0"  # ないならば0にする
if ans[0] == "0":
    ans = list(ans)
    ans[0] = "1"
    ans = "".join(ans)

if N == 1 and 1 in correspondence.keys():
    print(list(correspondence[1])[0])
    exit()

if 1 in correspondence.keys() and list(correspondence[1])[0] == 0:  # 1桁目が0に指定されているなら終わり
    print(-1)
else:
    print(ans)

# for i in range(1, 1000):
#     i = str(i)
#     flag = True
#     tmp = True
#     for k in correspondence.keys():
#         if k > len(i):
#             tmp = False
#             break
#         if i[k-1] != str(list(correspondence[i])[0]):
#             flag = False
#     if flag and tmp:
#         print(i)
#         exit()
# print(-1)

Buggy submission (verdict: Runtime Error):
from collections import defaultdict
N, M = map(int, input().split())  # ちょうどN桁, M個のクエリ
correspondence = defaultdict(set)
for _ in range(M):
    s, c = map(int, input().split())
    correspondence[s].add(c)
# print(correspondence)
for k, v in correspondence.items():  # 要素が複数あったら-1を出力
    if len(v) > 1:
        print(-1)
        exit()

ans = ""
for i in range(1, N+1):
    if i in correspondence.keys():  # 指定があるならばそれにする
        ans += str(list(correspondence[i])[0])
    else:
        ans += "0"  # ないならば0にする
if ans[0] == "0":
    ans = list(ans)
    ans[0] = "1"
    ans = "".join(ans)

if N == 1:
    print(list(correspondence[1])[0])
    exit()

if 1 in correspondence.keys() and list(correspondence[1])[0] == 0:  # 1桁目が0に指定されているなら終わり
    print(-1)
else:
    print(ans)

# for i in range(1, 1000):
#     i = str(i)
#     flag = True
#     tmp = True
#     for k in correspondence.keys():
#         if k > len(i):
#             tmp = False
#             break
#         if i[k-1] != str(list(correspondence[i])[0]):
#             flag = False
#     if flag and tmp:
#         print(i)
#         exit()
# print(-1)

Buggy submission (verdict: Runtime Error):
from collections import defaultdict
N, M = map(int, input().split())  # ちょうどN桁, M個のクエリ
correspondence = defaultdict(set)
for _ in range(M):
    s, c = map(int, input().split())
    correspondence[s].add(c)
# print(correspondence)
for k, v in correspondence.items():  # 要素が複数あったら-1を出力
    if len(v) > 1:
        print(-1)
        exit()

# correspondence = dict(sorted(correspondence.items(), key=lambda x: x[0]))
# print(correspondence)
ans = ""
for i in range(1, N+1):
    if i in correspondence.keys():
        ans += str(list(correspondence[i])[0])
    else:
        ans += "0"
if ans[0] == "0":
    ans = list(ans)
    ans[0] = "1"
    ans = "".join(ans)

if list(correspondence[1])[0] == 0:
    print(-1)
else:
    print(ans)