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):


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

def can_match(i, A, next_set):
    if not A[i]:
        return
    j = A[i][-1]

    if A[j][-1] == i:
        if i < j:
            next_set.add((i,j))
        else:
            next_set.add((j,i))

next_set = set()
for i in range(N):
    can_match(i, A, next_set)


day = 0
battle = 0
while next_set:
    day += 1
    current_set, next_set = next_set, set()
    for match in current_set:
        battle += 1
        del A[match[0]][-1]
        can_match(match[0], A, next_set)
        del A[1][-1]
        can_match(match[1], A, next_set)

if battle == N*(N-1)//2:
    print(day)
else:
    print(-1)

import sys
input = sys.stdin.readline
N = int(input())

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

def can_match(i, A, next_set):
    if not A[i]: 
        return
    j = A[i][-1] 

    if A[j][-1] == i: 
        if i < j: 
            next_set.add((i,j))
        else: 
            next_set.add((j,i))


next_set = set() 
for i in range(N):
    can_match(i, A, next_set)


day = 0
battle = 0
while next_set:
    day += 1
    current_set, next_set = next_set, set()
    for match in current_set:
        battle += 1
        del A[match[0]][-1]
        can_match(match[0], A, next_set)
        del A[1][-1]
        can_match(match[1], A, next_set)

if battle == N*(N-1)//2:
    print(day)
else:
    print(-1)

N = int(input())

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

def can_match(i, A, next_set):
    if not A[i]: 
        return
    j = A[i][-1] 

    if A[j][-1] == i: 
        if i < j: 
            next_set.add((i,j))
        else: 
            next_set.add((j,i))


next_set = set() 
for i in range(N):
    can_match(i, A, next_set)


day = 0
battle = 0
while next_set:
    day += 1
    current_set, next_set = next_set, set()
    for match in current_set:
        battle += 1
        del A[match[0]][-1]
        can_match(match[0], A, next_set)
        del A[1][-1]
        can_match(match[1], A, next_set)

if battle == N*(N-1)//2:
    print(day)
else:
    print(-1)

import sys
input = sys.stdin.readline
N = int(input())

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

def can_match(i, A, next_set):
    if not A[i]: 
        return
    j = A[i][-1] 

    if A[j][-1] == i: 
        if i < j: 
            next_set.add((i,j))
        else: 
            next_set.add((j,i))


next_set = set() 
for i in range(N):
    can_match(i, A, next_set)


day = 0
battle = 0
while next_set:
    day += 1
    current_set, next_set = next_set, set()
    for match in current_set:
        battle += 1
        del A[match[0]][-1]
        can_match(match[0], A, next_set)
        del A[match[1]][-1]
        can_match(match[1], A, next_set)

if battle == N*(N-1)//2:
    print(day)
else:
    print(-1)

Verdicts: Runtime Error

Runtime Error

Runtime Error

Accepted