Problem p02997 (python)

Fixed submission:
N, K = map(int, input().split())

if N == 2:
    if K == 0:
        print (1)
        print (1, 2)
        exit()
    else:
        print (-1)
        exit()

if N == 3:
    if K == 0:
        print (3)
        print (1, 2)
        print (2, 3)
        print (1, 3)
        exit()
    elif K == 1:
        print(2)
        print (1, 2)
        print (2, 3)
        exit()
    else:
        print (-1)
        exit()

if N == 4:
    if K == 0:
        print (6)
        for i in range(1, 5):
            for j in range(i + 1, 5):
                print (i, j)
        exit()
    elif K == 1:
        print (5)
        for i in range(1, 5):
            for j in range(i + 1, 5):
                if i == 3 and j == 4:
                    pass
                else:
                    print (i, j)
        exit()
    elif K == 2:
        print (3)
        for i in range(1, 4):
            print (i, i + 1)
        exit()
    elif K == 3:
        print (3)
        for i in range(2, 5):
            print (1, i)
        exit()
    else:
        print (-1)
        exit()

edge = [[0] * N for _ in range(N)] #1: 辺あり, 0: 辺なし
# print (edge) 

#必要な辺が多すぎるとき
if ((N - 1) * (N -2))//2 < K:
    print (-1)
    exit()

def output(lst): #リストをもらって答えをアウトプットする
    count = 0
    for i in lst:
        for j in i:
            if j == 1:
                count += 1
    print (count)
    for i in range(N):
        for j in range(N):
            if lst[i][j] == 1:
                if i <= j:
                    print (i + 1, j + 1)
                else:
                    print (j + 1, i + 1)
    return

if N >= 5:
    if False:
        for i in range(N): #多角形を作成
            edge[i][(i+1)%N] = 1
        for i in range(N - K):
            edge[i][(i+2)%N] = 1
        output(edge)
        # print (edge)
        exit()
    else: # K > Nのとき
        for i in range(1, N): #1からすべての点に伸ばし、トータルで(n-1)C2の組を作る
            edge[0][i] = 1
        #あと何組減らさないといけないか
        res = ((N - 1) * (N - 2)) //2 - K
        #組を減らす必要があるときTrue
        if res > 0:
            flag = True
        else:
            flag = False
        
        tmp = 0
        while flag:
            tmp += 1
            for i in range(1, N):
                if res == 0:
                    flag = False
                    output(edge)
                    exit()
                if i + tmp < N:
                    if i != i + tmp and edge[i][i + tmp] == 0 and edge[i + tmp][i] == 0:
                        edge[i][i + tmp] = 1
                        res -= 1
                else:
                    if i != ((i + tmp) % N) + 1 and edge[i][((i + tmp) % N) + 1] == 0 and edge[((i + tmp) % N) + 1][i] == 0:
                        edge[i][((i + tmp) % N) + 1] = 1
                        res -= 1
        output(edge)
        exit()

Buggy submission (verdict: Runtime Error):
N, K = map(int, input().split())

# if N == 2:
#     if K == 0:
#         print (1)
#         print (1, 2)
#         exit()
#     else:
#         print (-1)
#         exit()

# if N == 3:
#     if K == 0:
#         print (3)
#         print (1, 2)
#         print (2, 3)
#         print (1, 3)
#         exit()
#     elif K == 1:
#         print(2)
#         print (1, 2)
#         print (2, 3)
#         exit()
#     else:
#         print (-1)
#         exit()

# if N == 4:
    if K == 0:
        print (6)
        for i in range(1, 5):
            for j in range(i + 1, 5):
                print (i, j)
        exit()
    elif K == 1:
        print (5)
        for i in range(1, 5):
            for j in range(i + 1, 5):
                if i == 3 and j == 4:
                    pass
                else:
                    print (i, j)
        exit()
    elif K == 2:
        print (3)
        for i in range(1, 4):
            print (i, i + 1)
        exit()
    elif K == 3:
        print (3)
        for i in range(2, 5):
            print (1, i)
        exit()
    else:
        print (-1)
        exit()

edge = [[0] * N for _ in range(N)] #1: 辺あり, 0: 辺なし
# print (edge) 

#必要な辺が多すぎるとき
if ((N - 1) * (N -2))//2 < K:
    print (-1)
    exit()

def output(lst): #リストをもらって答えをアウトプットする
    count = 0
    for i in lst:
        for j in i:
            if j == 1:
                count += 1
    print (count)
    for i in range(N):
        for j in range(N):
            if lst[i][j] == 1:
                if i <= j:
                    print (i + 1, j + 1)
                else:
                    print (j + 1, i + 1)
    return

for i in range(1, N): #1からすべての点に伸ばし、トータルで(n-1)C2の組を作る
    edge[0][i] = 1
    #あと何組減らさないといけないか
res = ((N - 1) * (N - 2)) //2 - K
#組を減らす必要があるときTrue
if res == 0:
    output(edge)
    exit()

tmp = 0
while True:
    tmp += 1
    for i in range(1, N):
        if res == 0:
            # flag = False
            output(edge)
            exit()
        if i + tmp < N:
            if i != i + tmp and edge[i][i + tmp] == 0 and edge[i + tmp][i] == 0:
                edge[i][i + tmp] = 1
                res -= 1
        else:
            if i != ((i + tmp) % N) + 1 and edge[i][((i + tmp) % N) + 1] == 0 and edge[((i + tmp) % N) + 1][i] == 0:
                edge[i][((i + tmp) % N) + 1] = 1
                res -= 1

Buggy submission (verdict: Runtime Error):
N, K = map(int, input().split())

#必要な辺が多すぎるとき
if ((N - 1) * (N -2))//2 < K:
    print (-1)
    exit()

if N == 2:
    print (-1)
    exit()

if N == 3:
    if K == 0:
        print (3)
        print (1, 2)
        print (2, 3)
        print (3, 1)
        exit()
    elif K == 1:
        print(2)
        print (1, 2)
        print (2, 3)
        exit()
    else:
        print (-1)
        exit()

if N == 4:
    if K == 0:
        print (6)
        for i in range(1, 5):
            for j in range(i + 1, 5):
                print (i, j)
        exit()
    elif K == 1:
        print (5)
        for i in range(1, 5):
            for j in range(i + 1, 5):
                if i == 3 and j == 4:
                    pass
                else:
                    print (i, j)
        exit()
    elif K == 2:
        print (3)
        for i in range(1, 4):
            print (i, i + 1)
        exit()
    elif K == 3:
        print (3)
        for i in range(2, 5):
            print (1, i)
        exit()
    else:
        print (-1)
        exit()

edge = [[0] * N for _ in range(N)] #1: 辺あり, 0: 辺なし
# print (edge) 

def output(lst): #リストをもらって答えをアウトプットする
    count = 0
    for i in lst:
        for j in i:
            if j == 1:
                count += 1
    print (count)
    for i in range(N):
        for j in range(N):
            if lst[i][j] == 1:
                print (i + 1, j + 1)
    return

if N >= 5:
    if False:
        for i in range(N): #多角形を作成
            edge[i][(i+1)%N] = 1
        for i in range(N - K):
            edge[i][(i+2)%N] = 1
        output(edge)
        # print (edge)
        exit()
    else: # K > Nのとき
        for i in range(1, N): #1からすべての点に伸ばし、トータルで(n-1)C2の組を作る
            edge[0][i] = 1
        #あと何組減らさないといけないか
        res = ((N - 1) * (N - 2)) //2 - K
        #組を減らす必要があるときTrue
        if res > 0:
            flag = True
        else:
            flag = False
        
        tmp = 0
        while flag:
            tmp += 1
            for i in range(1, N):
                if res == 0:
                    flag = False
                    break
                if i + tmp < N:
                    if i != i + tmp and edge[i][i + tmp] == 0 and edge[i + tmp][i] == 0:
                        edge[i][i + tmp] = 1
                        res -= 1:
                else:
                    if i != (i + tmp) % N + 1 and edge[i][(i + tmp) % N + 1] == 0 and edge[(i + tmp) % N + 1][i] == 0:
                        edge[i][(i + tmp) % N + 1] = 1
                        res -= 1
        output(edge)
        exit()

Buggy submission (verdict: Runtime Error):
N, K = map(int, input().split())

#必要な辺が多すぎるとき
if ((N - 1) * (N -2))//2 < K:
    print (-1)
    exit()

if N == 2:
    print (-1)
    exit()

if N == 3:
    if K == 0:
        print (3)
        print (1, 2)
        print (2, 3)
        print (3, 1)
        exit()
    elif K == 1:
        print(2)
        print (1, 2)
        print (2, 3)
        exit()
    else:
        print (-1)
        exit()

if N == 4:
    if K == 0:
        print (6)
        for i in range(1, 5):
            for j in range(i + 1, 5):
                print (i, j)
        exit()
    elif K == 1:
        print (5)
        for i in range(1, 5):
            for j in range(i + 1, 5):
                if i == 3 and j == 4:
                    pass
                else:
                    print (i, j)
        exit()
    elif K == 2:
        print (3)
        for i in range(1, 4):
            print (i, i + 1)
        exit()
    elif K == 3:
        print (3)
        for i in range(2, 5):
            print (1, i)
        exit()
    else:
        print (-1)
        exit()

edge = [[0] * N for _ in range(N)] #1: 辺あり, 0: 辺なし
# print (edge) 

def output(lst): #リストをもらって答えをアウトプットする
    count = 0
    for i in lst:
        for j in i:
            if j == 1:
                count += 1
    print (count)
    for i in range(N):
        for j in range(N):
            if lst[i][j] == 1:
                print (i + 1, j + 1)
    return

if N >= 5:
    if False:
        for i in range(N): #多角形を作成
            edge[i][(i+1)%N] = 1
        for i in range(N - K):
            edge[i][(i+2)%N] = 1
        output(edge)
        # print (edge)
        exit()
    else: # K > Nのとき
        for i in range(1, N): #1からすべての点に伸ばし、トータルで(n-1)C2の組を作る
            edge[0][i] = 1
        #あと何組減らさないといけないか
        res = ((N - 1) * (N - 2)) //2 - K
        #組を減らす必要があるときTrue
        if res > 0:
            flag = True
        else:
            flag = False
        
        tmp = 0
        while flag:
            tmp += 1
            for i in range(1, N):
                if res == 0:
                    flag = False
                    break
                if i + tmp < N
                    if i != i + tmp and edge[i][i + tmp] == 0 and edge[i + tmp][i] == 0:
                        edge[i][i + tmp] = 1
                        res -= 1:
                else:
                    if i != (i + tmp) % N + 1 and edge[i][(i + tmp) % N + 1] == 0 and edge[(i + tmp) % N + 1][i] == 0:
                        edge[i][(i + tmp) % N + 1] = 1
                        res -= 1
        output(edge)
        exit()