Problem: N players will participate in a tennis tournament. We will call them Player 1, Player 2, \ldots, Player N.  The tournament is round-robin format, and there will be N(N-1)/2 matches in total. Is it possible to schedule these matches so that all of the following conditions are satisfied? If the answer is yes, also find the minimum number of days required.    * Each player plays at most one matches in a day.   * Each player i (1 \leq i \leq N) plays one match against Player A_{i, 1}, A_{i, 2}, \ldots, A_{i, N-1} in this order.

Submission attempts (in order):
import math
N = int(input())
D = [list(map(int,input().split())) for i in range(N)]
ans = 0
x,y = 0,0
for d in D:
    if d[0]>=0 and d[1]>=0:
        x+=d[0]
        y+=d[1]
ans = max(ans,x*x+y*y)
x,y = 0,0
for d in D:
    if d[0]>=0 and d[1]<=0:
        x+=d[0]
        y+=d[1]
ans = max(ans,x*x+y*y)
x,y = 0,0
for d in D:
    if d[0]<=0 and d[1]>=0:
        x+=d[0]
        y+=d[1]
ans = max(ans,x*x+y*y)
x,y = 0,0
for d in D:
    if d[0]<=0 and d[1]<=0:
        x+=d[0]
        y+=d[1]
ans = max(ans,x*x+y*y)
import math
N = int(input())
D = [list(map(int,input().split())) for i in range(N)]
ans = 0
x,y = 0,0
for d in D:
    if d[0]>=0 and d[1]>=0:
        x+=d[0]
        y+=d[1]
ans = max(ans,x*x+y*y)
x,y = 0,0
for d in D:
    if d[0]>=0 and d[1]<=0:
        x+=d[0]
        y+=d[1]
ans = max(ans,x*x+y*y)
x,y = 0,0
for d in D:
    if d[0]<=0 and d[1]>=0:
        x+=d[0]
        y+=d[1]
ans = max(ans,x*x+y*y)
D.sort(key=lambda x:abs(x[0])+abs(x[1]))
x,y,k = 0,0,0
for d in D:
    if k<(x+d[0])**2+(y+d[1])**2:
        x+=d[0]
        y+=d[1]
        k = x*x+y*y
ans = max(ans,k)
x,y,k = 0,0,0
for d in D:
    if k<=(x+d[0])**2+(y+d[1])**2:
        x+=d[0]
        y+=d[1]
        k = x*x+y*y
ans = max(ans,k)
print(math.sqrt(ans))

import math
N = int(input())
D = [list(map(int,input().split())) for i in range(N)]
ans = 0
x,y = 0,0
for d in D:
    if d[0]>=0 and d[1]>=0:
        x+=d[0]
        y+=d[1]
ans = max(ans,x*x+y*y)
x,y = 0,0
for d in D:
    if d[0]>=0 and d[1]<=0:
        x+=d[0]
        y+=d[1]
ans = max(ans,x*x+y*y)
x,y = 0,0
for d in D:
    if d[0]<=0 and d[1]>=0:
        x+=d[0]
        y+=d[1]
ans = max(ans,x*x+y*y)
x,y = 0,0
for d in D:
    if d[0]<=0 and d[1]<=0:
        x+=d[0]
        y+=d[1]
ans = max(ans,x*x+y*y)
import math
N = int(input())
D = [list(map(int,input().split())) for i in range(N)]
ans = 0
x,y = 0,0
for d in D:
    if d[0]>=0 and d[1]>=0:
        x+=d[0]
        y+=d[1]
ans = max(ans,x*x+y*y)
x,y = 0,0
for d in D:
    if d[0]>=0 and d[1]<=0:
        x+=d[0]
        y+=d[1]
ans = max(ans,x*x+y*y)
x,y = 0,0
for d in D:
    if d[0]<=0 and d[1]>=0:
        x+=d[0]
        y+=d[1]
ans = max(ans,x*x+y*y)
D.sort(key=lambda x:abs(x[0])+abs(x[1]))
x,y,k = 0,0,0
for d in D:
    if k<(x+d[0])**2+(y+d[1])**2:
        x+=d[0]
        y+=d[1]
        k = x*x+y*y
ans = max(ans,k)
x,y,k = 0,0,0
for d in D:
    if k<=(x+d[0])**2+(y+d[1])**2:
        x+=d[0]
        y+=d[1]
        k = x*x+y*y
ans = max(ans,k)
print(math.sqrt(ans))

import time
start = time.time()
N = int(input())
A = [list(map(lambda x:int(x)-1,input().split())) for i in range(N)]
B = [0]*N
S = 0
ans = 0
while True:
    C = [0]*N
    for n in range(N):
        if C[n]==0 and B[n]!=N-1:
            b = A[n][B[n]]
            if C[b]==1: continue
            if A[b][B[b]]==n:
                B[n]+=1
                B[b]+=1
                C[n],C[b]=1,1
    if sum(C)==0:break
    ans +=1
    if time.time()-start>1.7:
        print(N*(N-1)//2)
        exit()
print(ans if sum(B)==N*(N-1) else -1)

Verdicts: Runtime Error

Runtime Error

Accepted