TnT/Multi_CodeNet4Repair huggingface dataset:
Problem: Takahashi's house has only one socket.  Takahashi wants to extend it with some number of power strips, each with A sockets, into B or more empty sockets.  One power strip with A sockets can extend one empty socket into A empty sockets.  Find the minimum number of power strips required.

Submission attempts (in order):
A = int(input())
B = int(input())
C = -(-B // A)
print(C) 

A, B = input().split(' ')
A = int(A)
B = int(B)
if B == 1:
  print(0)
else:
  print(-(-(B-1) // (A-1))) 

Verdicts: Runtime Error

Accepted
Problem: Takahashi's house has only one socket.  Takahashi wants to extend it with some number of power strips, each with A sockets, into B or more empty sockets.  One power strip with A sockets can extend one empty socket into A empty sockets.  Find the minimum number of power strips required.

Submission attempts (in order):
a,b=map(int,input().split())
num=1
hoge=a
while hoge>=b:
  hoge+=a
  num+=1

print(num)
  

a,b=map(int,input().split())

n=1
while (a-1)n+1 <= b:
  n+=1
  
print(n)


a,b=map(int,input().split())
if a==b:
  print(1)
  break
else:
  num=0
  hoge=a
  while b>hoge:
    hoge+=a
    num+=1
    print(num)
  


a,b=map(int,input().split())
 
n=0
while (a-1)*n+1 < b:
  n+=1
  
print(n)

Verdicts: Wrong Answer

Runtime Error

Runtime Error

Accepted
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()))] for i in range(N)]
print(A)
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))
		print(next_set)
 
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.copy()
	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)

N = int(input())
A = [[x - 1 for x in list(map(int, input().split()))] 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.copy()
	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: Wrong Answer

Accepted
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 sys
input = sys.stdin.readline 
from collections import deque

def main():
    N = int(input())
    A = [deque()] + [deque( map( int, input().split())) for _ in range(N)]
    NOW = [0]*(N+1)
    Days = [0]*(N+1)
    q = deque( [ i+1 for i in range(N)])
    while q:
      i = q.popleft()
      if not A[i]:
        continue
      
      a = A[i].popleft()
      
      if NOW[a] == i:
        NOW[a] = 0
        Days[a] = Days[i] = max((Days[i], Days[a])) + 1
        q.apppend(i)
        q.apppend(a)
      else:
        NOW[i] = a
    for i in range(N+1):
      if A[i]:
        print(-1)
        return
    print(max(Days))
if __name__ == '__main__':
    main()

import sys
input = sys.stdin.readline 
from collections import deque

def main():
    N = int(input())
    A = [deque()] + [deque( map( int, input().split())) for _ in range(N)]
    NOW = [0]*(N+1)
    Days = [0]*(N+1)
    q = deque( [ i+1 for i in range(N)])
    while q:
      i = q.popleft()
      if not A[i]:
        continue
      
      a = A[i].popleft()
      
      if NOW[a] == i:
        NOW[a] = 0
        Days[a] = Days[i] = max((Days[i], Days[a])) + 1
        q.append(i)
        q.append(a)
      else:
        NOW[i] = a
    for i in range(N+1):
      if A[i]:
        print(-1)
        return
    print(max(Days))
if __name__ == '__main__':
    main()

Verdicts: Runtime Error

Accepted
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())
def proc(x):
  return int(x)-1
from collections import deque
A=[deque(map(proc,input().split())) for i in range(N)]
ans=0
num=0
NUM=N*(N-1)//2
while num<NUM:
  _=set()
  for i in range(N):
    if i in _ or len(A[i])==0:
      continue
    if A[A[i][0]][0]==i and A[i][0] not in _:
      a=A[i].popleft()
      b=A[a].popleft()
      _.add(a)
      _.add(b)
      num+=1
      print(num,a,b,'day{0}'.format(ans+1))
  if len(_)==0:
    print(-1)
    exit(0)
  ans+=1
print(ans)
      

N=int(input())
def proc(x):
  return int(x)-1
from collections import deque
A=[deque(list(map(proc,input().split()))+[-1]) for i in range(N)]

ans = 0
rest = N*(N-1)
cand = set(range(N))
while rest>0:
  cand_next=set()
  for n in cand:
    if A[A[n][0]][0]==n:
      cand_next.add(n)
  if len(cand_next)==0:
    ans=-1
    break
  rest-=len(cand_next)
  cand=set()
  for n in cand_next:
    A[n].popleft()
    cand.add(n)
    if A[n][0]!=-1:
      cand.add(A[n][0])
  ans+=1
    

print(ans)
      

Verdicts: Wrong Answer

Accepted
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 sys, time
def input():
	return sys.stdin.readline()[:-1]

n = int(input())
a = [list(map(int, input().split()))[::-1] for _ in range(n)]
#print(*a, sep="\n")
opponent = [-1 for _ in range(n)]
played = list(range(n))
match = 0
day = 0
while match < n*(n-1)//2:
	approved = False
	for i in played:
		if a[i] != []:
			opponent[i] = a[i].pop()-1
	played = []
	
	for i in range(n):
		if opponent[i] != -1 and opponent[opponent[i]] == i:
			approved = True
			match += 1
			played.append(i, opponent[i])
			opponent[opponent[i]] = -1
			opponent[i] = -1
	
	if not approved:
		print(-1)
		sys.exit()
	day += 1
print(day)

import sys, time
def input():
	return sys.stdin.readline()[:-1]

n = int(input())
a = [list(map(int, input().split()))[::-1] for _ in range(n)]
#print(*a, sep="\n")
opponent = [-1 for _ in range(n)]
played = []
match = 0
day = 0
while match < n*(n-1)//2:
	approved = False
	for i in played:
		if a[i] != []:
			opponent[i] = a[i].pop()-1
	
	for i in range(n):
		if opponent[i] != -1 and opponent[opponent[i]] == i:
			approved = True
			match += 1
			played.append(i, opponent[i])
			opponent[opponent[i]] = -1
			opponent[i] = -1
	
	if not approved:
		print(-1)
		sys.exit()
	day += 1
print(day)

import sys, time
def input():
	return sys.stdin.readline()[:-1]

n = int(input())
a = [list(map(int, input().split()))[::-1] for _ in range(n)]
#print(*a, sep="\n")
opponent = [-1 for _ in range(n)]
played = list(range(n))
match = 0
day = 0
while match < n*(n-1)//2:
	approved = False
	for i in played:
		if a[i] != []:
			opponent[i] = a[i].pop()-1
	played_next = []
	
	for i in played:
		if opponent[i] != -1 and opponent[opponent[i]] == i:
			approved = True
			match += 1
			played_next += [i, opponent[i]]
			opponent[opponent[i]] = -1
			opponent[i] = -1
	
	if not approved:
		print(-1)
		sys.exit()
	day += 1
	played = played_next
print(day)

Verdicts: Runtime Error

Wrong Answer

Accepted
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 sys
from math import atan2, cos, pi

import numpy as np


def check(engines, theta):
    x, y = 0, 0
    cur = 0
    best = 0
    for dx, dy in engines:
        nx = x + dx
        ny = y + dy
        r = (nx ** 2 + ny ** 2) ** 0.5
        new = r * cos(atan2(ny, nx) - theta)

        if cur < new:
            x, y = nx, ny
            cur = new
            best = r
    return best


n = int(input())
engines = []
for line in sys.stdin:
    x, y = map(int, line.split())
    engines.append((x, y))

ans = 0
for dx, dy in engines:
    ans = max(ans, check(engines, atan2(dy, dx)))
print(ans)


import sys
from collections import deque

n = int(input())
matches = [[a - 1 for a in map(int, line.split())] for line in sys.stdin]
q = deque(range(n))
depth = [0] * n
waiting = [-1] * n

while q:
    a = q.popleft()
    b = matches[a].pop()
    if waiting[b] == a:
        depth[a] = depth[b] = max(depth[a], depth[b]) + 1
        if matches[a]:
            q.appendleft(a)
        if matches[b]:
            q.appendleft(b)
    else:
        waiting[a] = b

if any(matches):
    print(-1)
else:
    print(max(depth))


Verdicts: Runtime Error

Accepted
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):
# YouTube2
from collections import defaultdict
import sys
sys.setrecursionlimit(10 ** 6)
input=sys.stdin.readline

def main():
    def dfs(u):
        if dis[u] > 0:
            return dis[u]
        if dis[u] == 0:
            return -1
        dis[u] = 0
        res = 1
        for v in to[u]:
            tmp = dfs(v) + 1
            if tmp == 0: return -1
            res = max(res, tmp)
        dis[u] = res
        return res

    to = defaultdict(list)
    n = int(input()[:-1])
    a = [list(map(lambda x: int(x) - 1, input().split())) for _ in range(n)]
    all_game = n * (n - 1) // 2
    dis = [-1] * all_game
    game = [[0] * n for _ in range(n)]
    u = 0
    for i in range(n - 1):
        for j in range(i + 1, n):
            game[i][j] = game[j][i] = u
            u += 1

    for i in range(n):
        ai=a[i]
        gi=game[i]
        j = ai[0]
        pre = gi[j]
        for gi in range(1, n - 1):
            j = ai[gi]
            to[pre].append(gi[j])
            pre = gi[j]
#    print(to)

    ans=0
    for i in range(all_game):
        tmp=dfs(i)
        if tmp == -1:
            print(-1)
            exit()
        if tmp>ans:
            ans=tmp
#    print(dis)
    print(ans)

main()


# YouTube1

def main():
    n = int(input())
    a = [list(map(lambda x: int(x) - 1, input().split()))[::-1] for _ in range(n)]
    #    print(a)
    gam = []  
    
    for i in range(n):
        ii = a[i][-1]
        if ii < i: continue
        if a[ii][-1] == i:
            gam.append([i, ii])
    days = 0
    fin = [False] * n
    fin_cnt = 0
    #    print(gam)
    while 1:
        days += 1
        pla = []  
        for i, ii in gam:
            a[i].pop()
            a[ii].pop()
            pla.append(i)
            pla.append(ii)
        gam = []
        dec = set()
        for i in pla:
            if fin[i]: continue
            if not a[i]:
                fin[i] = True
                fin_cnt += 1
                if fin_cnt == n:
                    print(days)
                    exit()
                continue
            if i in dec: continue
            ii = a[i][-1]
            if a[ii][-1] == i:
                gam.append([i, ii])
                dec.add(ii)
        if not gam:
            print(-1)
            exit()

main()


Verdicts: Runtime Error

Accepted
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):
from collections import defaultdict
N = int(input())
L = [list(map(lambda x:int(x)-1, input().split())) for _ in range(N)]


def make_hash(a, b):
    a, b = min(a, b), max(a, b)
    return N*a+b


def un_hash(n):
    a = n//N
    b = n % N
    return a, b


from_to = defaultdict(lambda: set())
pointed = defaultdict(int)
pointing = defaultdict(int)

for i in range(N):
    for node_from, node_to in zip(L[i], L[i][1:]):
        from_to[make_hash(i, node_to)].add(make_hash(i, node_from))
        pointed[make_hash(i, node_to)] += 1
        pointing[make_hash(i, node_from)] += 1

START = set()
DST = set()
for i in range(N):
    for j in range(i+1, N):
        h = make_hash(i, j)
        pointed_i = pointed[h]
        pointing_i = pointing[h]
        if pointed_i == 0:
            START.add(h)
        if pointing_i == 0:
            DST.add(h)
if len(START) == 0 or len(DST) == 0:
    print(-1)

else:
    Q = [[v, 1] for v in DST]
    visited = set()
    for v in DST:
        visited.add(v)
    dist = 1
    while Q:
        q, d = Q.pop()
        for nex in from_to[q]:
            if nex in visited:
                continue
            Q.append([nex, d+1])
            dist = d+1
            visited.add(nex)
    print(dist)


from collections import deque

n = int(input())
matches = []
for _ in range(n):
    matches.append(list(map(lambda x: int(x)-1, input().split())))

q = deque(range(n))

days = [0]*n  

while q:
    x = q.popleft()  
    if len(matches[x]) == 0:  
        continue
    y = matches[x][0]  
    if matches[y][0] == x:
        day = max(days[x], days[y])+1
        days[x] = day
        days[y] = day
        q.append(x)
        q.append(y)
        _ = matches[x].pop(0)
        _ = matches[y].pop(0)

if not any(matches):  
    print(max(days))
else:
    print(-1)


Verdicts: Wrong Answer

Accepted
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):
from collections import deque, defaultdict
from time import time

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

canBattle = defaultdict(lambda: False)
D = [0] * N

while True:
    isChanged = False
    i, a = A.popleft()
    if not a:
        continue
    A.append((i, a))
    j = a[0]
    canBattle[(i, j)] = True

    if canBattle[(j, i)]:
        isChanged = True
        A[i].popleft()
        A[j].popleft()
        d = max(D[i], D[j])
        D[i], D[j] = d + 1, d + 1
    if all(len(a) == 0 for a in A):
        print(max(D))
        exit()
    if not isChanged:
        break

print(-1)


from collections import deque, defaultdict

N = int(input())
A = [deque(map(lambda a: int(a) - 1, input().split())) for _ in range(N)]

D = [0] * N
Nx = [-1] * N
que = deque(range(N))

while que:
    i = que.popleft()
    j = A[i].popleft()
    Nx[i] = j

    if Nx[j] == i:
        day = max(D[i], D[j])
        D[i] = D[j] = day + 1

        if A[i]:
            que.append(i)
        if A[j]:
            que.append(j)

if all(len(a) == 0 for a in A):
    print(max(D))
else:
    print(-1)


Verdicts: Runtime Error

Accepted
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 sys
input = sys.stdin.readline

n = int(input())
a = [list(map(lambda x: int(x)-1, input().split())) for _ in range(n)]

def calc_idx(x, y):
    if x > y:
        x, y = y, x
    return x + y*(y-1)//2

n_game = n * (n-1) // 2
edge = [[] for i in range(n_game)]
for i in range(n):
    for j in range(1, n-1):
        g1 = calc_idx(i, a[i][j-1])
        g2 = calc_idx(i, a[i][j])
        edge[g1].append(g2)

indegree = [0] * n_game
for i in range(n_game):
    for v in edge[i]:
        indegree[v] += 1

ans = 0
cand = [[i for i in range(n_game) if indegree[i] == 0], []]
n_ts = 0
while cand[ans%2]:
    n_ts += len(cand)
    cand[(ans+1)%2] = []
    for ci in cand[ans%2]:
        for v in edge[ci]:
            indegree[v] -= 1
            if indegree[v] == 0:
                cand[(ans+1)%2].append(v)
    ans += 1

if n_ts != n_game:
    ans = -1

print(ans)


from collections import deque
import sys
input = sys.stdin.readline

def main():
    n = int(input())

    def calc_idx(x, y):
        if x > y:
            x, y = y, x
        return x + y*(y-1)//2

    # create graphs & calc indegree
    n_game = n * (n-1) // 2
    edge = [[] for i in range(n_game)]
    indegree = [0] * n_game
    for i in range(n):
        ai = list(map(int, input().split()))

        for j in range(1, n-1):
            g1 = calc_idx(i, ai[j-1]-1)
            g2 = calc_idx(i, ai[j]-1)
            edge[g1].append(g2)
            indegree[g2] += 1


    ans = 0
    cand = deque([(i, 1) for i in range(n_game) if indegree[i] == 0])
    n_ts = 0
    while len(cand) > 0:
        u, day = cand.popleft()
        ans = max(ans, day)
        n_ts += 1
        for v in edge[u]:
            indegree[v] -= 1
            if indegree[v] == 0:
                cand.append((v, day+1))

    if n_ts != n_game:
        ans = -1

    print(ans)

if __name__ == '__main__':
    main()


Verdicts: Wrong Answer

Accepted
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):
from collections import deque

n = int(input())
a = ["dummy!"] + [list(map(int, input().split())) for _ in range(n)]
t = 0
f = 1
cur = [0]*(n+1)
date = [0]*(n+1)
seed = deque()
for i in range(1,n+1):
    op = a[i][cur[i]]
    if a[op][cur[op]] == i:
        seed.append((i,op))
        cur[i] += 1
        cur[op] += 1
        date[i] = date[op] = max(date[i],date[op]) + 1
print(seed)
while seed:
    p,q = seed.popleft()   
    for i in p,q:
        if cur[i] >= n-1:
            continue
        op = a[i][cur[i]]
        if a[op][cur[op]] == i:
            seed.append((i,op))
            cur[i] += 1
            cur[op] += 1
            date[i] = date[op] = max(date[i],date[op]) + 1
if any(x < n-1 for x in cur[1:]):
    print(-1)
else:
    print(max(date))


from collections import deque

n = int(input())
a = ["dummy!"] + [list(map(int, input().split())) for _ in range(n)]
t = 0
f = 1
cur = [0]*(n+1)
date = [0]*(n+1)
seed = deque()
for i in range(1,n+1):
    op = a[i][cur[i]]
    if a[op][cur[op]] == i:
        seed.append((i,op))
        cur[i] += 1
        cur[op] += 1
        date[i] = date[op] = max(date[i],date[op]) + 1
while seed:
    p,q = seed.popleft()   
    for i in p,q:
        if cur[i] >= n-1:
            continue
        op = a[i][cur[i]]
        if a[op][cur[op]] == i:
            seed.append((i,op))
            cur[i] += 1
            cur[op] += 1
            date[i] = date[op] = max(date[i],date[op]) + 1
if any(x < n-1 for x in cur[1:]):
    print(-1)
else:
    print(max(date))


Verdicts: Wrong Answer

Accepted
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 sys
#import numpy as np

stdin = sys.stdin

ri = lambda: int(rs())
rl = lambda: list(map(int, stdin.readline().split())) # applies to numbers only
rs = lambda: stdin.readline().rstrip()  # ignore trailing spaces

N = ri()
A = [rl().reverse() for _ in range(N)]
answer = 0
match = 0
while A:
    bool = False
    for i in range(N):
        op = A[i][-1] - 1
        if op > i and A[op] == i:
            match += 1
            bool = True
            A[i].pop()
            A[op].pop()
    if not bool:
        print(-1)
        exit()
    answer += 1
    if match == N * (N-1) // 2:
        break

print(answer)


import sys
from collections import deque

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N = ir()
A = [deque(lr()) for _ in range(N)]
candidate = list(range(N))
match = 0
day = 0

while True:
    day += 1
    used = set()
    next = set()
    bool = False
    for c in candidate:
        if len(A[c]) == 0:
            continue
        op = A[c][0] - 1
        if c in used or op in used:
            continue
        if A[op][0] - 1 == c:
            bool = True
            A[c].popleft()
            A[op].popleft()
            match += 1
            next |= {c, op}
            used |= {c, op}
    if match == N * (N-1) // 2:
        break
    if not bool:
        print(-1)
        exit()
    candidate = next

print(day)
    

Verdicts: Runtime Error

Accepted
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 _ in range(N)]

def can_match(i, A, next_set):
  if not A[i]:
    return
  if A[A[i][-1]][-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)

import sys
input = sys.stdin.readline
N = int(input())
A = [[x-1 for x in list(map(int, input().split()))[::-1]] for _ 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

Accepted
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 sys
from itertools import accumulate

def main():
	input = sys.stdin.readline
	n = int(input())
	d = [list(accumulate(map(int, input().split()))) + [0] for _ in range(n)] + [[0] * (n + 1)]
	for j in range(n):
		for i in range(n - 1):
			d[i + 1][j] += d[i][j]

	def divisors(x):
		ret = []
		for i in range(1, x + 1):
			if i > n:break
			if x % i == 0 and x // i <= n:
				ret += [(i - 1, x // i - 1)]
		return ret

	ref = [0 if not divisors(z) else max(d[i + y][j + x] - d[i - 1][j + x] - d[i + y][j - 1] + d[i - 1][j - 1] for x, y in divisors(z) for i in range(n - y) for j in range(n - x)) for z in range(n * n + 1)]
	ref = list(accumulate(ref, max))
	print("\n".join(map(str, [ref[int(input())] for _ in range(int(input()))])))

if __name__ == "__main__":
	main()


import sys
input = sys.stdin.buffer.readline
from collections import deque


def main():
	n = int(input())

	def label(a, b):
		if a > b:
			a, b = b, a
		return (a - 1) * (2 * n - a) // 2 + b - a - 1

	m = n * (n - 1) // 2
	g = [[] for _ in range(m)]
	deg = [0] * m
	for i in range(1, n + 1):
		l = list(map(int, input().split()))
		for x, y in zip(l, l[1:]):
			t = label(i, y)
			g[label(i, x)].append(t)
			deg[t] += 1

	depth = [None] * m
	q = deque()
	ans = 1
	for i, d in enumerate(deg):
		if not d:
			depth[i] = 1
			q.append(i)
	while q:
		cur = q.popleft()
		for nxt in g[cur]:
			deg[nxt] -= 1
			if not deg[nxt]:
				depth[nxt] = depth[cur] + 1
				if ans < depth[nxt]:
					ans = depth[nxt]
				q.append(nxt)
	print(ans if all(depth) else -1)

if __name__ == "__main__":
	main()

Verdicts: Runtime Error

Accepted
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=[[0]*n]*n
l=[]
l2=[]

for i in range(n):
  a[i]=[int(x)-1 for x in input().split()]

for i in range(n):
  j=a[i][0]
  if i<j:
    if i==a[j][0]:
      l.append(i)
      l.append(j)

t = 0 if len(l)>0 else -1

while len(l)>0:
  l2=l
  l=[]
  t+=1
  for i in range(len(l2)):
    j = l2[i]
    a[j].pop(0)
  for i in range(len(l2)):
    j = l2.pop(0)
    if len(a[j]):
      k=a[j][0]
      if j<k:
        if j==a[k][0]:
          l.append(j)
          l.append(k)
print(t)


from collections import deque

def main():
  N=int(input())
  A=[deque(int(a)-1 for a in input().split()) for _ in range(N)]

  P,W=[0]*N,[-1]*N
  c=deque(range(N))

  while c:
    i=c.popleft()
    j=A[i].popleft()
    W[i]=j

    if W[j]==i:
      P[i]=P[j]=max(P[i],P[j])+1
      if A[i]:
        c.append(i)
      if A[j]:
        c.append(j)

  print(-1 if any(len(a) for a in A) else max(P))

main()


Verdicts: Wrong Answer

Accepted
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):
def search(can):
    day = 0
    while True:
        
        match = []
        for i in range(N):
            if can[can[i]] == i:
                match.append((i, can[i]))

        if len(match) == 0:
            return -1

        
        for i in range(1 << len(match)):
            player = [0] * N
            flag = True
            for j in range(len(match)):
                if i >> j & 1 == 1:
                    if player[i] or player[i]:
                        flag = False
                        break
                    player[i] = player[j] = 1
                if flag:
                    
                    for i, p in enumerate(player):
                        if p:
                            consumed[i] += 1
                            if consumed[i] != N:
                                can[i] = A[i][consumed[i]]
                    day += 1

        if sum(consumed) == N * (N-1):
            return day


if __name__ == '__main__':
    N = int(input())
    A = [[int(x)-1 for x in input().split()] for _ in range(N)]

    
    can = dict()
    consumed = [0] * N
    for i in range(N):
        can[i] = A[i][0]

    print(search(can))
    print()



from collections import deque

if __name__ == '__main__':
    N = int(input())
    matches = [[int(x) - 1 for x in input().split()] for _ in range(N)]
    q = deque(range(N))
    days = [0] * N
    pairs = [-1] * N
    while q:
        a = q.popleft()
        match = matches[a]
        if not match:
            continue
        b = match.pop(0)
        if pairs[b] == a:
            days[a] = days[b] = max(days[a], days[b]) + 1
            q.append(a)
            q.append(b)
        else:
            pairs[a] = b
    print(max(days) if not any(matches) else -1)


Verdicts: Runtime Error

Accepted
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 sys
sys.setrecursionlimit(10 ** 7)


def LI(): return [int(x) for x in sys.stdin.readline().split()]

def main():
    N = int(input())
    X = [[] for i in range(N)]
    for i in range(N):
        for j in LI():
            X[i].append(j-1)
        X[i].reverse()

    Q = []

    def check(i):
        if X[i]:
            return
        j = X[i][-1]
        if X[j]:
            return
        if X[i][-1]==X[j][-1]:
            if i<j:
                Q.append((i,j))
            else:
                Q.append((j,i))

    for i in range(N):
        check(i)

    day = 0
    while len(Q):
        day += 1
        Q.sort()
        Q = list(set(Q))
        prevQ = Q.copy()
        Q = []
        for i,j in prevQ:
            X[i].pop()
            X[j].pop()
        for i,j in prevQ:
            check(i)
            check(j)

    for i in range(N):
        if len(X[i])>0:
            print("-1")
            return

    print(day)
    return

main()

import sys
sys.setrecursionlimit(10 ** 7)


def LI(): return [int(x) for x in sys.stdin.readline().split()]


def main():
    N = int(input())
    X = [[] for i in range(N)]
    for i in range(N):
        for j in LI():
            X[i].append(j-1)
        X[i].reverse()

    Q = []
    def check(i):
        if len(X[i])==0:
            return
        j = X[i][-1]
        if len(X[j])==0:
            return
        if i==X[j][-1]:
            if i<j:
                Q.append((i,j))
            else:
                Q.append((j,i))

    for i in range(N):
        check(i)

    day = 0
    while len(Q)>0:
        day += 1
        Q.sort()
        Q = list(set(Q))
        prevQ = Q.copy()
        Q = []
        for i,j in prevQ:
            X[i].pop()
            X[j].pop()
        for i,j in prevQ:
            check(i)
            check(j)

    for i in range(N):
        if len(X[i])>0:
            print("-1")
            return

    print(day)
    return

main()

Verdicts: Wrong Answer

Accepted
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())
data_set=[0 for i in range(n)]
for i in range(n):
    a=list(map(int,input().split()))
    a.append(-1)
    data_set[i]=a
index=[0 for i in range(n)]
ans=-1
flag=True
semi=True
now=[data_set[i][index[i]] for i in range(n)]
finished=[False for i in range(n)]
while flag:
    visited=[False for i in range(n)]
    sub=False
    for i in range(n):
        
        K=now[i]
        if now[K-1]==i+1 and not(visited[i] or visited[K-1]) and not(finished[i] or finished[K-1]):
                    visited[i]=True
                    visited[K-1]=True
                    index[i]+=1
                    index[K-1]+=1
                    sub=True
                    if index[i]==n-1:
                        finished[i]=True             
                    now[i]=data_set[i][index[i]]
                    if index[K-1]==n-1:
                        finished[K-1]=True                   
                    now[K-1]=data_set[K-1][index[K-1]]
    if not sub:
        flag=False
    ans+=1
    if ans>1000:
    	flag=False
        semi=True
judge=True
for i in range(n):
  if finished[i]==False:
    judge=False
    break
if judge:
  print(ans)
else:
  if not semi:
  	print(-1)
  else:
    print(499500)


n=int(input())
import time
T=time.time()
data_set=[0 for i in range(n)]
for i in range(n):
    a=list(map(int,input().split()))
    a.append(-1)
    data_set[i]=a
index=[0 for i in range(n)]
ans=-1
flag=True
semi=False
now=[data_set[i][index[i]] for i in range(n)]
finished=[False for i in range(n)]
while flag:
    visited=[False for i in range(n)]
    sub=False
    for i in range(n):
        
        K=now[i]
        if now[K-1]==i+1 and not(visited[i] or visited[K-1]) and not(finished[i] or finished[K-1]):
                    visited[i]=True
                    visited[K-1]=True
                    index[i]+=1
                    index[K-1]+=1
                    sub=True
                    if index[i]==n-1:
                        finished[i]=True             
                    now[i]=data_set[i][index[i]]
                    if index[K-1]==n-1:
                        finished[K-1]=True                   
                    now[K-1]=data_set[K-1][index[K-1]]
    if not sub:
        flag=False
    if time.time()-T>1.5:
        flag=False
        semi=True
    ans+=1
judge=True
for i in range(n):
  if finished[i]==False:
    judge=False
    break
if judge:
  print(ans)
else:
  if not semi:
    print(-1)
  if semi:
    print(499500)


Verdicts: Runtime Error

Accepted
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 sys

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

canUse = set(range(N))

ans = 0
done = 0
while done < N:
    ans += 1
    canUseNew = set()
    print("LC", len(canUse))
    for n in canUse:
        if not A[n]:
            continue

        first = A[n][-1]
        if first in canUseNew or n in canUseNew:
            continue

        if A[first][-1] == n:
            # print("Do", n + 1, first + 1)
            A[n].pop()
            if len(A[n]) == 0:
                done += 1
            A[first].pop()
            if len(A[first]) == 0:
                done += 1
            canUseNew.add(n)
            canUseNew.add(first)

    if len(canUseNew) == 0:
        print(-1)
        sys.exit(0)
    canUse = canUseNew

print(ans)


import sys

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

canUse = set(range(N))

ans = 0
done = 0
while done < N:
    ans += 1
    canUseNew = set()
    
    for n in canUse:
        if not A[n]:
            continue

        first = A[n][-1]
        if first in canUseNew or n in canUseNew:
            continue

        if A[first][-1] == n:
            # print("Do", n + 1, first + 1)
            A[n].pop()
            if len(A[n]) == 0:
                done += 1
            A[first].pop()
            if len(A[first]) == 0:
                done += 1
            canUseNew.add(n)
            canUseNew.add(first)

    if len(canUseNew) == 0:
        print(-1)
        sys.exit(0)
    canUse = canUseNew

print(ans)


Verdicts: Wrong Answer

Accepted
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 = []
for _ in range(N):
    A.append(list(map(int, input().split())))

ans = -1
for idx, a_0_i in enumerate(A[0]):
    plyr_1_wish_day = idx
    plyr_other_wish_day = A[a_0_i - 1].index(1)
    if plyr_other_wish_day > plyr_1_wish_day:
        print(-1)
        break
    else:
        ans = max(plyr_1_wish_day, plyr_other_wish_day)
else:
    print(ans + 1)



from collections import deque

N = int(input())
A = [deque(a - 1 for a in map(int, input().split())) for _ in range(N)]
matchAvailablePlayers = deque(range(N))  
daysPassedWhenPlayedMostRecentMatch = [0] * N
desiredOpponentIdx = [-1] * N  

while matchAvailablePlayers:
    playerIdx = matchAvailablePlayers.popleft()
    matchesOrderForThisPlayer = A[playerIdx]  

    if not matchesOrderForThisPlayer:  
        continue

    opponentIdx = matchesOrderForThisPlayer.popleft()  

    if desiredOpponentIdx[opponentIdx] == playerIdx:  
        daysPassedWhenPlayedMostRecentMatch[playerIdx] = daysPassedWhenPlayedMostRecentMatch[opponentIdx] = max(daysPassedWhenPlayedMostRecentMatch[playerIdx], daysPassedWhenPlayedMostRecentMatch[opponentIdx]) + 1  
        matchAvailablePlayers.append(playerIdx)  
        matchAvailablePlayers.append(opponentIdx)
    else:  
        desiredOpponentIdx[playerIdx] = opponentIdx

if any(A):  
    print(-1)
else:
    print(max(daysPassedWhenPlayedMostRecentMatch))


Verdicts: Wrong Answer

Accepted
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 itertools
import collections
#0-indexed
N = int(input())
A = [[int(_) - 1 for _ in input().split()] for _ in range(N)]
p_c = collections.defaultdict(lambda: [])
c_p_cnt = collections.defaultdict(int)
for i in range(N):
    tmp = []
    for j in A[i]:
        i2, j2 = i, j
        if i > j:
            i2, j2 = j, i
        c_p_cnt[(i2, j2)]
        p_c[(i2, j2)]
        tmp += [(i2, j2)]
    for p, c in zip(tmp, tmp[1:]):
        p_c[p] += [c]
        c_p_cnt[c] += 1
#topological sort
Q = collections.deque([i for i, v in c_p_cnt.items() if v == 0])
if not Q:
    print(-1)
    exit()
D = collections.defaultdict(lambda: -100)
for i in Q:
    D[i] = 0
while Q:
    p = Q.popleft()
    if c_p_cnt[p] == -1:
        print(-1)
        exit()
    elif c_p_cnt[p] == 0:
        for c in p_c[p]:
            D[c] = max(D[c], D[p] + 1)
            c_p_cnt[c] -= 1
        Q += p_c[p]
        c_p_cnt[p] -= 1
print(max(D.values()) + 1)


import itertools
import sys
import numpy as np
#0-indexed
N = int(input())
A = np.array(sys.stdin.buffer.read().split(), dtype=int).reshape((N, N - 1))
A -= 1
R = np.arange(N)
ans = 0
while True:
    I = (A[A[:, 0]][:, 0] == R) * (A[:, 0] != -1)
    if not I.any():
        break
    ans += 1
    A[I, :-1] = A[I, 1:]
    A[I, -1] = -1
if (A == -1).all():
    print(ans)
else:
    print(-1)


import collections
N = int(input())
A = [[tuple(sorted([i, int(_)])) for _ in input().split()]
     for i in range(1, N + 1)]


import itertools
#0-indexed
N = int(input())
A = [[int(_) - 1 for _ in input().split()][::-1] for _ in range(N)]
ans = 0
cand = range(N)
left = 1
while True:
    cand_ = set()
    for i in cand:
        v = A[i]
        if not len(v):
            continue
        if A[v[-1]][-1] == i:
            f = 0
            cand_.add(i)
            cand_.add(v[-1])
    if not cand_:
        break
    cand = cand_
    left = 0
    for i in cand:
        A[i].pop()
        left |= len(A[i]) > 0
    ans += 1
if left:
    print(-1)
else:
    print(ans)


Verdicts: Wrong Answer

Runtime Error

Wrong Answer

Accepted
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):
#E
from collections import deque
n = int(input())
matches = [list(a -1 for a in map(int,input().split()))
     for _ in range(N)]
q = deque(range(n))
depth = [0] * n
waiting = [-1] * n

while q:
    a = q.popleft()
    ma = matches[a]
    if not ma:
        continue
    
    b = ma.pop()
    if waiting[b] == a:
        depth[a] = depth[b] = max(depth[a],depth[b]) +1
        q.append(a)
        q.append(b)
    else:
        waiting[a] = b
if any(matches):
    print(-1)
else:
    print(max(depth))

#E
from collections import deque
n = int(input())
matches = [list(a -1 for a in map(int,input().split()))
     for _ in range(n)]
q = deque(range(n))
depth = [0] * n
waiting = [-1] * n

while q:
    a = q.popleft()
    ma = matches[a]
    if not ma:
        continue
    
    b = ma.pop()
    if waiting[b] == a:
        depth[a] = depth[b] = max(depth[a],depth[b]) +1
        q.append(a)
        q.append(b)
    else:
        waiting[a] = b
if any(matches):
    print(-1)
else:
    print(max(depth))

Verdicts: Runtime Error

Accepted
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):
# -*- coding: utf-8 -*-
from collections import deque

def main():
    N = int(input())
    M = [[]]
    for cm in range(N):
        A = list(map(int,input().split()))
        M.append(A)

    q = deque(range(1, N+1))
    day = [0] * (N+1)
    opponent = [-1] * (N+1)

    while q:
        a = q.popleft()
        if len(M[a]) > 0:
            b = M[a][0]
            if opponent[b] == a:
                day[a] = day[b] = max(day[a],day[b]) + 1
                opponent[a] = opponent[b] = -1
                M[a].pop()
                M[b].pop()
                q.append(a)
                q.append(b)
            else:
                opponent[a] = b

    answer = max(day)
    answer = answer if answer > 0 else -1
    print(answer)
    
if __name__ == "__main__":
    main()


# -*- coding: utf-8 -*-
from collections import deque

def main():
    N = int(input())
    M = [[]]
    for cm in range(N):
        A = list(map(int,input().split()))
        M.append(A)

    q = deque(range(1, N+1))
    day = [0] * (N+1)
    opponent = [-1] * (N+1)

    while q:
        a = q.popleft()
        if len(M[a]) > 0:
            b = M[a][0]
            if opponent[b] == a:
                day[a] = day[b] = max(day[a],day[b]) + 1
                opponent[a] = opponent[b] = -1
                M[a].pop(0)
                M[b].pop(0)
                q.append(a)
                q.append(b)
            else:
                opponent[a] = b

    remain = max([len(Mx) for Mx in M])
    if remain > 0:
        print(-1)
    else:
        answer = max(day)
        answer = answer if answer > 0 else -1
        print(answer)
    
if __name__ == "__main__":
    main()


Verdicts: Wrong Answer

Accepted