Problem p02925 (python)

Fixed submission:
from collections import* #defaultdict Counter deque appendleft
import sys
input=sys.stdin.readline
import time
t=time.time()

def main():
    n=int(input())
    d=[[]]+[deque(map(int,input().split()))for i in n*[0]]

    c=0
    while any(d):
        c+=1
        f=0
        skip=set()
        for i in range(1,n+1):
            if i not in skip:
                if d[i]:
                    if d[i][0] in skip:
                        continue
                    elif i==d[d[i][0]][0]:
                        oppo=d[i].popleft()
                        d[oppo].popleft()
                        skip.add(oppo)
                        skip.add(i)
                        f=1

        if not f:
            print(-1)
            exit()
        if abs(time.time()-t)>1:
            print(n*(n-1)//2)
            exit()

    print(c)

if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
from collections import* #defaultdict Counter deque appendleft
import sys
input=sys.stdin.readline

def main():
    n=int(input())
    d=[[]]+[deque(map(int,input().split()))for i in range(n)]

    c=0
    while any(map(len,d)):
        c+=1
        f=0
        skip=set()
        for i in range(1,n+1):
            if i not in skip.copy():
                #if d[i]:
                if d[i][0] in skip:
                    continue
                elif i==d[d[i][0]][0]:
                    oppo=d[i].popleft()
                    d[oppo].popleft()
                    skip.add(oppo)
                    skip.add(i)
                    f=1
                        
        if not f:
            print(-1)
            exit()

    print(c)

if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
from collections import* #defaultdict Counter deque appendleft
import sys
input=sys.stdin.readline

def main():
    n=int(input())
    d=[deque(map(int,input().split()))for i in range(n)]

    c=0
    immortalskip=set()
    while any(d):
        c+=1
        f=0
        skip=set()
        for i in range(n):
            if i not in skip.copy() and i not in immortalskip:
                if d[i]:
                    if d[i][0] in skip:
                        continue
                    elif i==d[d[i][0]-1][0]:
                        oppo=d[i].popleft()
                        d[oppo].popleft()
                        skip.add(oppo)
                        skip.add(i)
                        f=1
                else:
                    immortalskip.add(i)
#            print(c,i,d,skip,f)
        if not f:
            print(-1)
            exit()

    print(c)

if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
from collections import* #defaultdict Counter deque appendleft
import sys
input=sys.stdin.readline

def main():
    n=int(input())
    d=[deque(map(int,input().split()))for i in range(n)]

    c=0
    immortalskip=set()
    while any(d):
        c+=1
        f=0
        skip=set()
        for i in range(n):
            if i not in skip.copy() and i not in immortalskip:
                if d[i]:
                    if d[i][0] in skip:
                        continue
                    elif i==d[d[i][0]-1][0]:
                        oppo=d[i].popleft()
                        d[oppo].popleft()
                        skip.add(oppo)
                        skip.add(i)
                        f=1
                else:
                    immortalskip.add(i)
#            print(c,i,d,skip,f)
        if not f:
            print(-1)
            exit()

    print(c)

if __name__ == '__main__':
    main()