Problem p02925 (python)

Fixed submission:
import sys
input = sys.stdin.readline

N = int(input())
A = [list(map(int,input().split())) for i in range(N)]

for i in range(1,N+1):
    for j in range(N-1):
        a,b = sorted([i,A[i-1][j]])

        A[i-1][j] = a*(N-1)+b-N-1

E=[[] for i in range(N*N)]
EREV=[[] for i in range(N*N)]

EDGEIN=[0]*(N*N)

for i in range(N):
    for j in range(1,N-1):
        E[A[i][j-1]].append(A[i][j])
        EREV[A[i][j]].append(A[i][j-1])
        EDGEIN[A[i][j]]+=1

from collections import deque
Q=deque()

for i in range((N-1)**2):
    if EDGEIN[i]==0 and len(E[i])>0:
        Q.append(i)

TOP=[]
while Q:
    x=Q.pop()
    TOP.append(x)
    for to in E[x]:
        EDGEIN[to]-=1
        if EDGEIN[to]==0:
            Q.append(to)


H=[1]*(N**2)
for x in TOP[::-1]:

    for fr in EREV[x]:
            
        if H[fr]<H[x]+1:
            H[fr]=H[x]+1

if min(EDGEIN)<0 or max(EDGEIN)>0:
    print(-1)
else:
    print(max(H))

Buggy submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline

N = int(input())
A = [list(map(int,input().split())) for i in range(N)]

for i in range(1,N+1):
    for j in range(N-1):
        a,b = sorted([i,A[i-1][j]])

        A[i-1][j] = a*(N-1)+b-N-1

E=[set() for i in range(N*N)]
EREV=[set() for i in range(N*N)]

for i in range(N):
    for j in range(1,N-1):
        E[A[i][j-1]].add(A[i][j])
        EREV[A[i][j]].add(A[i][j-1])

from collections import deque
Q=deque()

H=[0]*N**N

for i in range(N*N):
    if E[i]==set() and len(EREV[i])>0:
        Q.append(i)
        H[i]=1

while Q:
    x=Q.pop()

    for fr in EREV[x]:
        H[fr]=max(H[x]+1,H[fr])
        if H[fr]>N*N:
            print(-1)
            sys.exit()
        Q.append(fr)
        

if max(H)==0:
    print(-1)
else:
    print(max(H))

Buggy submission (verdict: Runtime Error):
N = int(input())
A = [list(map(int,input().split())) for i in range(N)]

for i in range(1,N+1):
    for j in range(N-1):
        a,b = sorted([i,A[i-1][j]])

        A[i-1][j] = a*(N-1)+b-N-1

E=[set() for i in range(N*N)]
EREV=[set() for i in range(N*N)]

for i in range(N):
    for j in range(1,N-1):
        E[A[i][j-1]].add(A[i][j])
        EREV[A[i][j]].add(A[i][j-1])

from collections import deque
Q=deque()

H=[0]*N**N

for i in range(N*N):
    if E[i]==set() and len(EREV[i])>0:
        Q.append(i)
        H[i]=1

while Q:
    x=Q.pop()

    for fr in EREV[x]:
        H[fr]=max(H[x]+1,H[fr])
        Q.append(fr)

if max(H)==0:
    print(-1)
else:
    print(max(H))

Buggy submission (verdict: Runtime Error):
N = int(input())
A = [list(map(int,input().split())) for i in range(N)]

for i in range(1,N+1):
    for j in range(N-1):
        a,b = sorted([i,A[i-1][j]])

        A[i-1][j] = a*(N-1)+b-N-1

E=[[] for i in range(N*N)]
EREV=[[] for i in range(N*N)]

for i in range(N):
    for j in range(1,N-1):
        E[A[i][j-1]].append(A[i][j])
        EREV[A[i][j]].append(A[i][j-1])

from collections import deque
Q=deque()

H=[0]*N**N

for i in range(N*N):
    if E[i]==[] and len(EREV[i])>0:
        Q.append(i)
        H[i]=1

while Q:
    x=Q.pop()

    for fr in EREV[x]:
        H[fr]=max(H[x]+1,H[fr])
        Q.append(fr)

if max(H)==0:
    print(-1)
else:
    print(max(H))

Buggy submission (verdict: Runtime Error):
N = int(input())
A = [list(map(int,input().split())) for i in range(N)]

for i in range(1,N+1):
    for j in range(N-1):
        a,b = sorted([i,A[i-1][j]])

        A[i-1][j] = a*(N-1)+b-N-1

E=[[] for i in range(N*N)]
EREV=[[] for i in range(N*N)]

for i in range(N):
    for j in range(1,N-1):
        E[A[i][j-1]].append(A[i][j])
        EREV[A[i][j]].append(A[i][j-1])

from collections import deque
Q=deque()

H=[0]*N**N

for i in range(N*N):
    if E[i]==[] and len(EREV[i])>0:
        Q.append(i)
        H[i]=1

while Q:
    x=Q.pop()

    for fr in EREV[x]:
        H[fr]=max(H[x]+1,H[fr])
        Q.append(fr)

if max(H)==0:
    print(-1)
else:
    print(max(H))