Problem:


Score : 500 points

Problem Statement

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.

Constraints

3 \leq N \leq 1000

1 \leq A_{i, j} \leq N

A_{i, j} \neq i

A_{i, 1}, A_{i, 2}, \ldots, A_{i, N-1} are all different.

Input

Input is given from Standard Input in the following format:

N
A_{1, 1} A_{1, 2} \ldots A_{1, N-1}
A_{2, 1} A_{2, 2} \ldots A_{2, N-1}
:
A_{N, 1} A_{N, 2} \ldots A_{N, N-1}

Output

If it is possible to schedule all the matches so that all of the conditions are satisfied, print the minimum number of days required; if it is impossible, print -1.

Sample Input 1

3
2 3
1 3
1 2

Sample Output 1

3

All the conditions can be satisfied if the matches are scheduled for three days as follows:

Day 1: Player 1 vs Player 2

Day 2: Player 1 vs Player 3

Day 3: Player 2 vs Player 3

This is the minimum number of days required.

Sample Input 2

4
2 3 4
1 3 4
4 1 2
3 1 2

Sample Output 2

4

All the conditions can be satisfied if the matches are scheduled for four days as follows:

Day 1: Player 1 vs Player 2, Player 3 vs Player 4

Day 2: Player 1 vs Player 3

Day 3: Player 1 vs Player 4, Player 2 vs Player 3

Day 4: Player 2 vs Player 4

This is the minimum number of days required.

Sample Input 3

3
2 3
3 1
1 2

Sample Output 3

-1

Any scheduling of the matches violates some condition.



Sample input:
3
2 3
1 3
1 2


Sample output:
3

Submission status: Wrong Answer

Solution code:
# pypy ver. 計算量はせいぜいO(n^2) < 10**6 程度だと想定したのだけれど、そうではないのか？それともほかの理由？
def judge_and_count(A_lst, already_played, played_count):
    count = 0
    roop_count = 0 ##########
    while sum(played_count) < N*(N-1): # 10**3  sumに10**3
        new_play = False
        for i in range(1, N+1): # 10**3
            roop_count += 1 ##########
            if roop_count > 10**7: #####
                return -1###########
            if len(A_lst[i]) > 0 and already_played[i] == 0:
                p1 = i
                p2 = A_lst[i][0]
                if len(A_lst[p2]) == 0:
                    return -1
                if already_played[p2] == 1:
                    continue
                if A_lst[p2][0] == p1:
                    already_played[p1] = 1
                    already_played[p2] = 1
                    A_lst[p1].pop(0)
                    A_lst[p2].pop(0)
                    played_count[p1] += 1
                    played_count[p2] += 1
                    new_play = True
        count += 1

        for i in range(len(already_played)): # 10**3
            already_played[i] = 0
        if not new_play:
            return -1
    
    return count


N = int(input())

already_played = [0]*(N+1)
played_count = [0]*(N+1)

A_lst = [[]]


for i in range(N):
    A_low = list(map(int, input().split()))
    A_lst.append(A_low)


ans = judge_and_count(A_lst, already_played, played_count)

print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque
N = int(input())
A = [deque(list(map(int, input().split()))) for _ in range(N)]
ans = 0
t = 0
for i in range(10**3):
    print(A)
    for k in range(N):
        if A[k] != deque([]):
            if A[k][0] == 0:
                A[k].popleft()
        else:
            t += 1
    if t == N:
        break
    t = 0
    for j in range(N):
        if A[j]:
            tmp = A[j][0]
        else:
            continue
        if tmp-1 > j and A[tmp-1][0] -1 == j:
            A[j][0] = 0
            A[tmp-1][0] = 0
            print(A,ans)
    ans += 1
if ans == 999:
    print(-1)
    exit()
print(ans-1)

Submission status: Wrong Answer

Solution code:
n = int(input())
l = [[] for _ in range(n)]
for i in range(n):
    l[i] = list(map(int,input().split()))
i=0
#al = [[] for j in range(3)]
u = [0]*n
d = 0
preunc = -1
unc = 0
while True:
    aset = set()
    for p in range(n):  
        unc = 0      
        if u[p] == n-1:
            pass
        elif p in aset:
            pass
        else:
            v = l[p][u[p]] - 1
            if p == l[v][u[v]]-1:
                u[p]+=1
                u[v]+=1
                aset.add(v)
            else:
                unc +=1
        for w in range(n):
            if u[w] != n-1:
                break
            else:
                print(max(u)+1)
                exit()
    #print(preunc,unc)
    if preunc == unc:
        print(-1)
        #print('nochange')
        break
    else:
        preunc = unc
    d+=1
    if d>499500: #(1000*999/2)
        print(-1)
        break

Submission status: Wrong Answer

Solution code:

# Setup
import sys
import collections
sys.setrecursionlimit(4100000)
a = int(input())
b = [collections.deque([int(i) for i in input().split()]) for _ in range(a)]
counter = 0
yesno = True
while len(b) != 0:
    found = False
    done = [False] * len(b)
    for i in range(len(b)):
        if len(b[i]) >= 1 and not done[i]:
            target = b[i][0]-1
            if not done[target] and len(b[target]) >= 1:
                if b[target][0] == i+1:
                    b[i].popleft()
                    b[target].popleft()
                    done[i] = True
                    done[target] = True
                    found = True
    if not found:
        break
    counter += 1
for a in b:
    if a != []:
        yesno = False
if not yesno:
    print(-1)
else:
    print(counter)

Submission status: Wrong Answer

Solution code:
# pypy ver. 計算量はせいぜいO(n^2) < 10**6 程度だと想定したのだけれど、そうではないのか？それともほかの理由？
def judge_and_count(A_lst, already_played, played_count):
    count = 0
    roop_count = 0 ##########
    while sum(played_count) < N*(N-1): # 10**3
        new_play = False
        for i in range(1, N+1): # 10**3
            roop_count += 1 ##########
            if roop_count > 3*10**6: #####
                return -1###########
            if len(A_lst[i]) > 0 and already_played[i] == 0:
                p1 = i
                p2 = A_lst[i][0]
                if len(A_lst[p2]) == 0:
                    return -1
                if already_played[p2] == 1:
                    continue
                if A_lst[p2][0] == p1:
                    already_played[p1] = 1
                    already_played[p2] = 1
                    A_lst[p1].pop(0)
                    A_lst[p2].pop(0)
                    played_count[p1] += 1
                    played_count[p2] += 1
                    new_play = True
        count += 1

        for i in range(len(already_played)): # 10**3
            already_played[i] = 0
        if not new_play:
            return -1
    
    return count


N = int(input())

already_played = [0]*(N+1)
played_count = [0]*(N+1)

A_lst = [[]]


for i in range(N):
    A_low = list(map(int, input().split()))
    A_lst.append(A_low)


ans = judge_and_count(A_lst, already_played, played_count)

print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
l = [[] for _ in range(n)]
for i in range(n):
    l[i] = list(map(int,input().split()))
i=0
al = [[] for j in range(3)]
u = [0]*n
d = 0
preunc = -1
unc = 0
while True:
    aset = set()
    for p in range(n):  
        unc = 0      
        if u[p] == n-1:
            pass
        elif p in aset:
            pass
        else:
            v = l[p][u[p]] - 1
            if p == l[v][u[v]]-1:
                u[p]+=1
                u[v]+=1
            else:
                unc +=1
        for w in range(n):
            if u[w] != n-1:
                break
            else:
                print(max(u)+1)
                exit()
    #print(preunc,unc)
    if preunc == unc:
        print(-1)
        #print('nochange')
        break
    else:
        preunc = unc
    d+=1
    if d>499500: #(1000*999/2)
        print(-1)
        break

Submission status: Wrong Answer

Solution code:
import sys
N = int(input())
a = [list(map(int, input().split())) for _ in range(N)]
ans = 0
tmp = 0
total = (N * (N - 1)) // 2

for i in range(N):
    sLst = [0] * N
    for j in range(N):
        if len(a[j]) != 0 and len(a[a[j][0] - 1]) != 0:
            if sLst[j] == 0 and sLst[a[j][0] - 1] == 0:
                if a[a[j][0] - 1][0] == j + 1:
                    sLst[a[j][0] - 1] = a[a[j][0] - 1].pop(0)

                    sLst[j] = a[j].pop(0)
                    tmp += 1
                    total -= 1
    if total == 0:
        ans += 1
        break

    if tmp == 0:
        print(-1)
        exit()
    ans += 1

print(ans)

Submission status: Wrong Answer

Solution code:
def main():

    n = int(input())
    listA=[] #appendのために宣言が必要
    for i in range(n):
        listA.append(list(map(int,input().split())))
    
    daied = 0
    days = 0
    error = 0

    for i in range(10000):
        shiai = [0]*n
        count = 0
        """
        if days < i-1:
            error = 1
            break
        else:
        """
        for j in range(n-1):
            if 0 < len(listA[j]):
                a = listA[j][0]
                if 0 < len(listA[a-1]):
                    b = listA[a-1][0]-1
                    if( j == b and shiai[j] == 0 and shiai[a-1] == 0):
                        listA[j].pop(0)
                        listA[a-1].pop(0)
                        shiai[j] = 1
                        shiai[a-1] = 1
                        days = i
        
        for j in range(n):
            count += shiai[j]
        
        if count == 0:
            error = 1
            break

    if error == 0:
        print(days+1)
    else:
        print(-1)

    

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
# pypy ver. 計算量はせいぜいO(n^2) < 10**6 程度だと想定したのだけれど、そうではないのか？それともほかの理由？
def judge_and_count(A_lst, already_played, played_count):
    count = 0
    roop_count = 0 ##########
    while sum(played_count) < N*(N-1): # 10**3
        new_play = False
        for i in range(1, N+1): # 10**3
            roop_count += 1 ##########
            if roop_count > 10**6:
                return -1
            if len(A_lst[i]) > 0 and already_played[i] == 0:
                p1 = i
                p2 = A_lst[i][0]
                if len(A_lst[p2]) == 0:
                    return -1
                if already_played[p2] == 1:
                    continue
                if A_lst[p2][0] == p1:
                    already_played[p1] = 1
                    already_played[p2] = 1
                    A_lst[p1].pop(0)
                    A_lst[p2].pop(0)
                    played_count[p1] += 1
                    played_count[p2] += 1
                    new_play = True
        count += 1

        for i in range(len(already_played)): # 10**3
            already_played[i] = 0
        if not new_play:
            return -1
    
    return count


N = int(input())

already_played = [0]*(N+1)
played_count = [0]*(N+1)

A_lst = [[]]


for i in range(N):
    A_low = list(map(int, input().split()))
    A_lst.append(A_low)


ans = judge_and_count(A_lst, already_played, played_count)

print(ans)

Submission status: Wrong Answer

Solution code:
A=[[1,2],[2]]
N=int(input())
A=[]
for _ in range(N):
    A.append(list(map(int,input().split(" "))))
buf=[0 for _ in range(N)]
day=0
i=0
while day<N**2:
    for i in range(N):
        if type(A[i]) is list:
            if A[A[i][0]-1][0]==i+1:
                if i>A[i][0]-1:
                    buf2=A[i][0]-1
                    if len(A[i])==1:
                        A[i]=0
                    else:
                        A[i].pop(0)
                    if len(A[buf2])==1:
                        A[buf2]=0
                    else:
                        A[buf2].pop(0)
                else:
                    buf2=i
                    if len(A[A[i][0]-1])==1:
                        A[A[i][0]-1]=0
                    else:
                        A[A[i][0]-1].pop(0)
                    if len(A[buf2])==1:
                        A[buf2]=0
                    else:
                        A[buf2].pop(0)
    day+=1
    if A==buf:
        print(day)
        break
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
#ABC139 E

import sys
import math
import bisect
sys.setrecursionlimit(1000000000)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

n = I()
a = [deque() for _ in range(n)]
for i in range(1,n+1):
    x = LI()
    for j in x:
        a[i-1].append((i,j))
ans = 0
for j in range(2*n):
    f = defaultdict(lambda : 0)
    flg = False
    flg2 = False
    for i in range(n):
        if len(a[i]) == 0:
            continue
        (x,y) = a[i].popleft()
        flg2 = True
        if f[(x,y)] == 0 and f[(y,x)] == 1:
                flg = True
                f[(y,x)] = 0
        elif f[(x,y)] == 0:
            f[(x,y)] = 1
    if flg:
        ans += 1
    if not flg2:
        print(ans)
        quit()
    for k,l in f.items():
        if l != 0:
            a[k[0]-1].appendleft(k)
for i in a:
    if len(i) != 0:
        print(-1)
        quit()
print(n*(n-1)//2)

Submission status: Wrong Answer

Solution code:
input = raw_input

N = int(input())
n = N

data= [ [int(elm) - 1 for elm in input().split(" ")] for i in range(N)]
#print(data)

nexts = [0 for _ in range(n)]
goal = [N-1 for _ in range(n)]

day = 1
all_game = 0
end_game = n * (n-1) // 2
nows_tmp = [False for _ in range(n)]
while True:
    done = False
    #nows = []
    #nows = nows_tmp.copy()
    nows = [False for _ in range(n)]

    for jibun_i in range(n):
        next_i = nexts[jibun_i]
        #if next_i == n-1 or jibun_i in nows:
        if next_i == n-1 or nows[jibun_i]:
            continue

        #print(jibun_i, next_i)
        aite_i = data[jibun_i][next_i]

        #if data[aite_i][nexts[aite_i]] == jibun_i and aite_i not in nows:
        if data[aite_i][nexts[aite_i]] == jibun_i and not nows[aite_i]:
            done = True
            nexts[jibun_i] += 1
            nexts[aite_i] += 1
            nows += [jibun_i, aite_i]
            all_game += 1

    #if goal == nexts:
    if all_game == end_game:
        out = day
        break

    if not done:
        out = -1
        break
    
    day += 1

print(out)

Submission status: Wrong Answer

Solution code:
def main():

    n = int(input())
    listA=[] #appendのために宣言が必要
    for i in range(n):
        listA.append(list(map(int,input().split())))
    
    daied = 0
    days = 0
    error = 0

    for i in range(n):
        shiai = [0]*n
        count = 0
        """
        if days < i-1:
            error = 1
            break
        else:
        """
        for j in range(n-1):
            if 0 < len(listA[j]):
                a = listA[j][0]
                if 0 < len(listA[a-1]):
                    b = listA[a-1][0]-1
                    if( j == b and shiai[j] == 0 and shiai[a-1] == 0):
                        listA[j].pop(0)
                        listA[a-1].pop(0)
                        shiai[j] = 1
                        shiai[a-1] = 1
                        days = i
        
        for j in range(n):
            count += shiai[j]
        
        if count == 0:
            error = 1
            break

    if error == 0:
        print(days+1)
    else:
        print(-1)

    

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
N = int(input())
A = [list(map(int,input().split())) for _ in range(N)]
used = [0] * N
count = 0
while (min(used) != N-1 ):

    temp = [-2] * N
    for i in range(N):
        now = used[i] 
        if used[i] == N-1:
            continue
        else:
            if temp[i] == -2:
                if temp[A[i][now]-1] == -2:
                    temp[A[i][now]-1] = i
                    used[i] += 1
                    temp[i] = -1
                else:
                    continue
            elif temp[i] != -1:
                if A[i][now]-1 == temp[i]:
                    used[i] += 1
                    temp[i] = -1
                else:
                    print(-1)
                    count = -100
                    break
            else:
                continue
    count += 1
    if count < 0:
        break
if count > 0:
    print(count)

Submission status: Wrong Answer

Solution code:
#-------------------------------------------------------------------
import sys
def p(*_a):
  _s=" ".join(map(str,_a))
  #print(_s)
  sys.stderr.write(_s+"\n")
#-------------------------------------------------------------------

n = int(input())						# 5

from collections import deque

Q=[]
for _ in range(n):
	a = [(int(i)-1) for i in input().split()]			# 1 2 3 4 5 ...
	q = deque()
	q.extend(a)
	Q.append(q)


#p(Q)
#for _ in range(n):
#	p(Q[_],Q[_][0],Q[_][1])


count=1
skip=[]

while count<=n:
	
	skip=[]
	for i in range(n):
		if len(Q[i])==0: continue
		a=Q[i][0]
		if skip.count(i)>0: continue
		if skip.count(a)>0: continue
		if Q[a][0]==i:
			Q[i].popleft()
			Q[a].popleft()
			skip.append(i)
			skip.append(a)
#		p(count, i, a, Q)
	
	x=0
	for i in range(n):
		x+=len(Q[i])
	
	if x==0: break
	count+=1
	

if count>n:
	print(-1)
else:
	print(count)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = [list(map(int, input().split())) for i in range(N)]
day = 0
H = [0] * N
yesterday = [i for i in range(N)]

while A != [[0]] * N:
  #print([[0]] * N)
  today = []
  for i in range(len(yesterday)):
    #print(A, i)
    p = yesterday[i]
    if A[p][0] > i:
      if (A[A[p][0] - 1][0] == p + 1): #対戦可能な場合対戦。
        x = A[p][0]
        A[p].pop(0)
        A[x - 1].pop(0)
        today.append(p)
        today.append(x-1)
        if len(A[p]) == 0:
          A[p].append(0)
        if len(A[x-1]) == 0:
          A[x-1].append(0)
        #print(A)
  day += 1
  #print(Q, A)
  #print(finish)
  if len(today) == 0: #もし終わらないのに試合ができないなら-1で終了させる
    print("-1")
    quit()
  yesterday = today
  
print(day + 1)

Submission status: Wrong Answer

Solution code:
N = int(input())
days = 0  # 日数
tmp = []  # 対戦候補表（先頭ほど消化優先度高い）
done = []  # 1日の中ですでに対戦予定の入ったプレーヤ
T = [['' for _ in range(N - 1)] for __ in range(N)]  # 対戦表
flag = True  # 対戦が進行可能の場合、false
flag2 = True  # 対戦を1つ以上こなしてループした場合にはfalse
c = 0  # 対戦表追加初期値
for n in range(N):
    T[n] = input().split()
while c < N - 1 or tmp != []:
    if flag2:
        for r in range(N):  # 直近対戦表追加
            if r + 1 < int(T[r][c]):
                tmp.append(str(r + 1) + T[r][c])
            else:
                tmp.append(T[r][c] + str(r + 1))
        c += 1
        flag = True
    flag2 = True
    for t in tmp:  # 対戦表の中でこなせる試合を探して消化
        if tmp.count(t) == 2 and not (t[0] in done or t[1] in done):
            flag = False
            flag2 = False
            done.extend(list(t))
            tmp = [_ for _ in tmp if _ != t]
    if flag:
        print('-1')
        exit()
    if not flag2:
        days += 1
    done = []
print(str(days))

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(int(1e6))

N = int(input())

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

count = 0
play = [0]
while play:
    play = []
    for i in A:
        if i in play:
            continue
        if not A[i]:
            continue
        j = A[i][0]
        if j in play:
            continue
        if not A[j]:
            continue
        if A[j][0] == i:
            play.append(i)
            play.append(j)
    if play:
        for i in play:
            del A[i][0]
        count += 1

print(count)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = []
for i in range(N):
	l = list(map(int,input().split()))
	A.append([i-1 for i in l])
match_num = [0 for i in range(N)]
match_dict = {}
idx = 0
for i in range(N-1):
	for j in range(i+1, N):
		match_dict[(i,j)] = idx
		idx += 1
M = idx
edge = [[] for i in range(M)]
parent = [[] for i in range(M)]
edge_dict = {}
for i in range(N):
	for j in range(N-2):
		a = A[i][j]
		a_nxt = A[i][j+1]
		x,y = min(i,a), max(i,a)
		z,w = min(i,a_nxt), max(i,a_nxt)
		m1 = match_dict[(x,y)]
		m2 = match_dict[(z,w)]
		edge[m1].append(m2)
		parent[m2].append(m1)
		edge_dict[min(m1, m2)*M + max(m1, m2)] = 0
finish = [False for i in range(M)]
candidate = [(i, 0) for i in range(M) if len(parent[i]) == 0]
longest = 0
while len(candidate) > 0:
	x = candidate.pop()
	c,d = x[0], x[1]
	finish[c] = True
	flg = False
	for e in edge[c]:
		x,y = min(c,e), max(c,e)
		dict_idx = x*M + y
		edge_dict[dict_idx] += 1
		if edge_dict[dict_idx] > 2:
			flg = True
			break
		candidate.append((e, d+1))
		longest = max(longest, d+1)
	if flg:
		break


all_finished = all(finish)
if all_finished:
	print(longest + 1)
else:
	print(-1)

Submission status: Wrong Answer

Solution code:
def main():
    n =int(input())
    seiyaku = []
    seiyaku_rev = []
    owata = []
    for i in range(n):
        seiyaku.append([int(x) for x in input().split()])
        seiyaku_rev.append([-1] * n)
        for j in range(n-1):
            seiyaku_rev[i][seiyaku[i][j] - 1] = j
        owata.append([-1] * (n-1))
    finished = 0
    day = 1
    cand =  [[0,0]]
    schedule = {0:0}
    max_day = 1
    already_done = [-1] * n
    while finished < n*(n-1) // 2:
        if len(cand) > n*(n-1) // 2:
            print(-1)
            return
        i, ord_i = cand[-1]
        j = seiyaku[i][ord_i] - 1
        ord_j = seiyaku_rev[j][i]
        flag = False
        if not ord_j == 0:
            if owata[j][ord_j - 1] == -1:
                if not j in schedule:
                    schedule[j] = n-1
                if schedule[j] < ord_j - 1:
                    print(-1)
                    return
                schedule[j] = ord_j - 1
                cand.append([j, ord_j - 1])
                continue
            else:
                day = max([owata[j][ord_j - 1] + 1, day])
                flag = True
        else:
            flag = True
        if flag:
            finished += 1
            day = max([day, ])
            owata[i][ord_i] = day
            owata[j][ord_j] = day
            already_done[i] = ord_i
            already_done[j] = ord_j
            cand.pop()
            if day > max_day:
                max_day = day
        if len(cand) == 0:
            if finished == n*(n-1) // 2:
                break
            else:
                min_ad = n*(n-1) // 2
                min_i = 0
                for i in range(n):
                    if already_done[i] < min_ad:
                        min_ad = already_done[i]
                        min_i = i
                cand.append([min_i, min_ad + 1])
                schedule = {min_i : min_ad + 1}
                day = 1 if min_ad == -1 else owata[min_i][min_ad] + 1
    print(max_day)
    return


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
from collections import deque

N = int(input())
A = [list(reversed(list(map(lambda x:int(x)-1,input().split())))) for i in range(N)]
B = {i:row for i,row in enumerate(A)}

end = 0
for k in range(8000):
    used = set()
    dels = []
    for i,q in B.items():
        if not q: continue
        if i in used: continue
        j = q[-1]
        if j in used: continue
        if A[j][-1] == i:
            A[j].pop()
            A[i].pop()
            used.add(i)
            used.add(j)
            if not A[i]:
                end += 1
                dels.append(i)
            if not A[j]:
                end += 1
                dels.append(j)
    if not used:
        print(-1)
        exit()
    if end == N:
        print(k+1)
        exit()
    for i in dels:
        del B[i]

print(N*(N+1)//2)

Submission status: Wrong Answer

Solution code:
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
	#print(opponent)
	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
	#print(opponent)
	if not approved:
		print(-1)
		sys.exit()
	day += 1
print(day)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
#ABC139 E

import sys
import math
import bisect
sys.setrecursionlimit(1000000000)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

n = I()
a = [deque() for _ in range(n)]
for i in range(1,n+1):
    x = LI()
    for j in x:
        a[i-1].append((i,j))
ans = 0
for j in range(10*n+1):
    f = defaultdict(lambda : 0)
    flg = False
    flg2 = False
    for i in range(n):
        if len(a[i]) == 0:
            continue
        (x,y) = a[i].popleft()
        flg2 = True
        if f[(x,y)] == 0 and f[(y,x)] == 1:
                flg = True
                f[(y,x)] = 0
        elif f[(x,y)] == 0:
            f[(x,y)] = 1
    if flg:
        ans += 1
    if not flg2:
        print(ans)
        quit()
    for k,l in f.items():
        if l != 0:
            a[k[0]-1].appendleft(k)
for i in a:
    if len(i) != 0:
        print(-1)
        quit()

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
n = int(input())
league = []
match = {0:0}
ll = []
for i in range(n):
    league.append(list(map(int, input().split())))
    match[i+1] = league[i][-1]
    ll.append(n-3)
ans = 0
cnt = 0
tmp = [0]
while cnt < n and tmp:
    tmp = []
    for k,v in match.items():
        if match[v] == k:
            tmp.append(v)
    for i in tmp:
        if ll[i-1] < 0:
            cnt += 1
            match[i] = 0
        else:
            match[i] = league[i-1][ll[i-1]]
            ll[i-1] -= 1
    ans += 1
print(ans if n == cnt else -1)

Submission status: Wrong Answer

Solution code:
N = int(input())
A_list = [[int(x)-1 for x in input().split()] for _ in range(N)]
cnt = 0
g = [-1] * N
while(cnt < 10):
    need_break = 1
    loop = 1
    for u, to_lst in enumerate(A_list):
        if (g[u] != -1 or len(to_lst) == 0): continue
        g[u] = to_lst.pop(0)
    for i, to in enumerate(g):
        need_break *= int(to == -1)
        if (to != -1 and i == g[to]):
            g[i] = -1
            g[to] = -1
            loop = 0
    if (need_break):
        break
    if (loop):
        print(-1)
        exit()
    cnt += 1
print(cnt)

Submission status: Wrong Answer

Solution code:
def main():

    n = int(input())
    listA=[] #appendのために宣言が必要
    for i in range(n):
        listA.append(list(map(int,input().split())))
    
    daied = 0
    days = 0
    error = 0

    for i in range(n):
        shiai = [0]*n
        count = 0
        """
        if days < i-1:
            error = 1
            break
        else:
        """
        for j in range(n-1):
            if 0 < len(listA[j]):
                a = listA[j][0]
                if 0 < len(listA[a-1]):
                    b = listA[a-1][0]-1
                    if( j == b and shiai[j] == 0 and shiai[a-1] == 0):
                        listA[j].pop(0)
                        listA[a-1].pop(0)
                        shiai[j] = 1
                        shiai[a-1] = 1
                        if days <= i:
                            days = i
        
        for j in range(n):
            count += shiai[j]
        
        if count == 0:
            error = 1
            break

    if error == 0:
        print(days+1)
    else:
        print(-1)

    

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import copy

n = int(input())
li = [0] * n
for i in range(n):
    li[i] = list(map(int, input().split()))
t = 0
r = 0
x = []
while t == 0 and x != li:
    x = copy.deepcopy(li)
    p = 10000
    for i, j in enumerate(li):
        if len(j) != 0:
            if li[j[0] - 1][0] == i + 1:
                if p == 10000:
                    p = i
                    r += 1
                elif p < i:
                    p = i
                else:
                    p = 10000
                li[j[0] - 1].pop(0)
                li[i].pop(0)
    for i, j in enumerate(li):
        t = 1
        if len(j) != 0:
            t *= 0
if x == li:
    print(-1)
else:
    print(r + 1)

Submission status: Wrong Answer

Solution code:
from collections import deque
N=int(input())
M=(N*(N-1))//2
D=dict()
tmp=0
X=[(0,0) for i in range(M)]
G=[[] for i in range(M)]
H=[[] for i in range(M)]
for i in range(1,N+1):
    for j in range(1,N+1):
        if i<j:
            D[(i,j)]=tmp
            X[tmp]=(i,j)
            tmp+=1
A=[[int(j) for j in input().split()] for i in range(N)]
for i in range(N):
    for j in range(N-2):
        p1=D[(i+1,A[i][j])] if i+1<A[i][j] else D[(A[i][j],i+1)]
        p2=D[(i+1,A[i][j+1])] if i+1<A[i][j+1] else D[(A[i][j+1],i+1)]
        G[p1].append(p2)
        H[p2].append(p1)
K=[0 for i in range(M)]
for i in range(M):
    for p in G[i]:
        K[p]+=1
q=deque(i for i in range(M) if K[i]==0)
res=[]
while q:
    v1=q.popleft()
    res.append(v1)
    for v2 in G[v1]:
        K[v2]-=1
        if K[v2]==0:
            q.append(v2)
#print(D)
#print(res)
if len(res)<M:
    print(-1)
    exit()
ans=[0 for i in range(M)]
ans[res[0]]=1
for i in res[1:]:
    for j in H[i]:
        ans[i]=max(ans[i],ans[j]+1)
print(max(ans))

Submission status: Wrong Answer

Solution code:
def scheduler(N, A):
    days = N - 1
    now = 1
    last = A[now - 1][-1]
    finished = [last]
    while days<10:
        x = A[last - 1].index(now)
        if x == N - 2:
            break
        days += N - 2 - x
        now = last
        finished.append(last)
        last = A[last -1][-1]
        if last in finished:
            return(-1)
    return(days)

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

print(scheduler(N, A))

Submission status: Wrong Answer

Solution code:
N=int(input())

A=[list(map(int,input().split())) for i in range(N)]
A.append([-1 for i in range(N*(N-1)//2)])

s=1
d=0
c=0
while s!=0:
    s=0
    d+=1
    o=list(range(N))
    for i in o:
        if A[A[i][0]-1][0]==i+1 and A[i][0]>i:
            s+=1
            c+=1
            o[A[i][0]-1]=N
            A[A[i][0]-1].pop(0)
            if A[A[i][0]-1]==[]:
                A[A[i][0]-1].append(-1)
            A[i].pop(0)
            if A[i]==[]:
                A[i].append(-1)


if c!=N*(N-1)/2:
    print(-1)
else:
    print(d)