Problem p02925 (python)

Fixed submission:
from collections import deque
import copy, sys

input = sys.stdin.readline
n = int(input())
a = [deque(map(lambda x: int(x) - 1, input().split())) for _ in range(n)]
q = deque()
pre_q = deque()
day = 0

def check(p, q):
    if not len(a[p]):
        return
    o = a[p][0]
    if p == a[o][0]:
        if p > o: p, o = o, p
        if not (p, o) in q:
            q.append((p, o))

fin = []
for p in range(n):
    if not p in fin:
        o = a[p][0]
        if p == a[o][0]:
            q.append((p, o))
            fin += [p, o]

while q:
    day += 1
    pre_q = copy.copy(q)
    q.clear()
    for (p, o) in pre_q:
        a[p].popleft()
        a[o].popleft()
        check(p, q)
        check(o, q)

if all(map(len, a)):
    print(-1)
else:
    print(day)

Buggy submission (verdict: Runtime Error):
import sys
from collections import deque
n = int(input())
a = [deque(map(lambda x:int(x) - 1, input().split())) for _ in range(n)]
q = deque()
pre_q = deque()
day = 0


def check(p, q):
    if not len(a[p]) == 0:
        o = a[p][0]
        if a[o][0] == p:
            if p < o and not (p, o) in q:
                q.append((p, o))
            elif not (o, p) in q:
                q.append((o, p))

for p in range(n):
    check(p, q)

while len(q) > 0:
    day += 1
    pre_q = q.copy()
    q.clear()
    for (p, o) in pre_q:
        a[p].popleft()
        a[o].popleft()
        check(p, q)
        check(o, q)


for i in range(n):
    if not len(a[i]) == 0:
        print(-1)
        sys.exit()

print(day)

Buggy submission (verdict: Runtime Error):
import sys
from collections import deque
n = int(input())
a = [deque(map(lambda x:int(x) - 1, input().split())) for _ in range(n)]
q = deque()
pre_q = deque()
day = 0


def check(p, q):
    if len(a[p]) == 0: return 0
    o = a[p][0]
    if a[o][0] == p:
        if p < o and not (p, o) in q:
            q.append((p, o))
        elif not (o, p) in q:
            q.append((o, p))

for p in range(n):
    check(p, q)

while len(q) > 0:
    day += 1
    pre_q = q.copy()
    q.clear()
    for (p, o) in pre_q:
        a[p].popleft()
        a[o].popleft()
        check(p, q)
        check(o, q)


for i in range(n):
    if not len(a[i]) == 0:
        print(-1)
        sys.exit()

print(day)

Buggy submission (verdict: Runtime Error):
import sys
from collections import deque
n = int(input())
a = [deque(map(lambda x:int(x) - 1, input().split())) for _ in range(n)]
q = deque()
pre_q = deque()
day = 0


def check(p, q):
    if len(a[p]) == 0: return
    o = a[p][0]
    if a[o][0] == p:
        if p < o and not (p, o) in q:
            q.append((p, o))
        elif not (o, p) in q:
            q.append((o, p))

for p in range(n):
    check(p, q)

while len(q) > 0:
    day += 1
    pre_q = q.copy()
    q.clear()
    for (p, o) in pre_q:
        a[p].popleft()
        a[o].popleft()
        check(p, q)
        check(o, q)


for i in range(n):
    if not len(a[i]) == 0:
        print(-1)
        sys.exit()

print(day)

Buggy submission (verdict: Runtime Error):
import sys, collections
n = int(input())
a = [collections.deque(map(lambda x:int(x) - 1, input().split())) for _ in range(n)]
q = collections.deque()
pre_q = collections.deque()
day = 0


def check(p, q):
    if len(a[p]) == 0: return
    o = a[p][0]
    if a[o][0] == p:
        if p < o and not (p, o) in q:
            q.append((p, o))
        elif not (o, p) in q:
            q.append((o, p))

for p in range(n):
    check(p, q)

while len(q) > 0:
    day += 1
    pre_q = q.copy()
    q.clear()
    for (p, o) in pre_q:
        a[p].popleft()
        a[o].popleft()
        check(p, q)
        check(o, q)


for i in range(n):
    if len(a[i]) != 0:
        print(-1)
        sys.exit()

print(day)