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:
import sys
sys.setrecursionlimit(10**7)
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def SI(): return sys.stdin.readline().strip()
INF = 10 ** 18
MOD = 10 ** 9 + 7


def conv(n,a,b):
    return a*(2*n-a)//2+b-a

def main(): 
    N = II()
    A = [[] for _ in range(N)]
    for i in range(N):
        A[i] = LI()

    # print(A)
    # return

    from collections import defaultdict
    outs = defaultdict(list)
    ins = defaultdict(int)
    for i in range(N):
        # prev_v = (i+1, A[i][0]) if i+1 < A[i][0] else (A[i][0], i+1)
        for j in range(1,N-1):
            # prev_v = (i+1, A[i][j-1]) if i+1 < A[i][j-1] else (A[i][j-1], i+1)
            # next_v = (i+1, A[i][j]) if (i+1 < A[i][j]) else (A[i][j] , i+1)
            # prev_v = conv(N, prev_v[0], prev_v[1])
            # next_v = conv(N, next_v[0], next_v[1])
            prev_v = (min(i+1, A[i][j-1]), max(i+1, A[i][j-1]))
            next_v = (min(i+1, A[i][j]), max(i+1, A[i][j]))
            outs[prev_v].append(next_v)
            ins[next_v] += 1

    # print(outs, ins)
    # return

    from collections import deque
    q = deque()
    for i in range(1, N+1):
        j = A[i-1][0]
        if i < j and ins[(i, j)] == 0:
            q.append([(i, j), 0])

    if not q:
        print(-1)
        return

    # q = deque(v1 for v1 in range(1, N+1) if ins[v1] == 0)
    res = []
    depth_max = 0
    while q:
        v1, depth = q.popleft()
        res.append(v1)
        for v2 in outs[v1]:
            ins[v2] -= 1
            if ins[v2] == 0:
                q.append([v2, depth+1])
                depth_max = max(depth_max, depth+1)
    if q:
        print(-1)
        return
    else:
        print(depth_max+1)
        return


main()

Submission status: Wrong Answer

Solution code:
import collections as cl
def l(): #intのlist
    return list(map(int,input().split()))
def onem(): #Nとかの取得
    return int(input())
n = onem()

on = []
de = cl.deque()
ppp = [n-2 for i in range(n)]
for i in range(n):
    k = l()
    for j in range(n-1):
        k[j] -= 1
    de.append([k[-1],i])
    on.append(cl.deque(k))


co = 1
ddd = cl.deque()
de.append([-1,-1])
cl = [1 for i in range(n)]
for aaa in range(n**2):
    kkk = de.popleft()
    if kkk[0] == -1 and kkk[1] == -1:
        if sum(cl) == n:
            co = -1
            break
        if len(de) == 0:
            break
        de.append([-1,-1])
        co += 1
        cl = [1 for i in range(n)]

    elif ppp[kkk[0]] != -1 and on[kkk[0]][ppp[kkk[0]]] == kkk[1] and cl[kkk[0]] and cl[kkk[1]]:
        cl[kkk[0]] = 0
        cl[kkk[1]] = 0
        ppp[kkk[0]] -= 1
        ppp[kkk[1]] -= 1
        if ppp[kkk[1]] != -1 and ppp[kkk[0]] != 0 and on[kkk[1]][ppp[kkk[1]]] == kkk[0] and on[kkk[0]][ppp[kkk[0]]] == kkk[1]:
            de.append([on[kkk[1]][ppp[kkk[1]]],kkk[1]])
        else:
            if len(on[kkk[1]]) != 0:
                de.append([on[kkk[1]][ppp[kkk[1]]],kkk[1]])
            if len(on[kkk[0]]) != 0:
                de.append([on[kkk[0]][ppp[kkk[0]]],kkk[0]])


print(co)

Submission status: Wrong Answer

Solution code:

import collections as cl
def l(): #intのlist
    return list(map(int,input().split()))
def onem(): #Nとかの取得
    return int(input())
n = onem()

on = []
de = cl.deque()
ppp = [n-2 for i in range(n)]
for i in range(n):
    k = l()
    for j in range(n-1):
        k[j] -= 1
    k.reverse()
    de.append([k[0],i])
    on.append(cl.deque(k))


co = 1
ddd = cl.deque()
de.append([-1,-1])
cl = [1 for i in range(n)]
for aaa in range(n**2):
    kkk = de.popleft()
    if kkk[0] == -1 and kkk[1] == -1:
        if sum(cl) == n:
            co = -1
            break
        if len(de) == 0:
            break
        de.append([-1,-1])
        co += 1
        cl = [1 for i in range(n)]

    elif ppp[kkk[0]] != -1 and on[kkk[0]][ppp[kkk[0]]] == kkk[1] and cl[kkk[0]] and cl[kkk[1]]:
        cl[kkk[0]] = 0
        cl[kkk[1]] = 0
        ppp[kkk[0]] -= 1
        ppp[kkk[1]] -= 1
        if ppp[kkk[1]] != -1 and ppp[kkk[0]] != 0 and on[kkk[1]][ppp[kkk[1]]] == kkk[0] and on[kkk[0]][ppp[kkk[0]]] == kkk[1]:
            de.append([on[kkk[1]][ppp[kkk[1]]],kkk[1]])
        else:
            if len(on[kkk[1]]) != 0:
                de.append([on[kkk[1]][ppp[kkk[1]]],kkk[1]])
            if len(on[kkk[0]]) != 0:
                de.append([on[kkk[0]][ppp[kkk[0]]],kkk[0]])


print(co)

Submission status: Wrong Answer

Solution code:
import collections as cl
def l(): #intのlist
    return list(map(int,input().split()))
def onem(): #Nとかの取得
    return int(input())
n = onem()

on = []
de = cl.deque()
ppp = [n-2 for i in range(n)]
for i in range(n):
    k = l()
    for j in range(n-1):
        k[j] -= 1
    k.reverse()
    de.append([k[0],i])
    on.append(cl.deque(k))


co = 1
ddd = cl.deque()
de.append([-1,-1])
cl = [1 for i in range(n)]
for aaa in range(n**2):
    kkk = de.popleft()
    if kkk[0] == -1 and kkk[1] == -1:
        if sum(cl) == n:
            co = -1
            break
        if len(de) == 0:
            break
        de.append([-1,-1])
        co += 1
        cl = [1 for i in range(n)]

    elif ppp[kkk[0]] != 0 and on[kkk[0]][ppp[kkk[0]]] == kkk[1] and cl[kkk[0]] and cl[kkk[1]]:
        cl[kkk[0]] = 0
        cl[kkk[1]] = 0
        ppp[kkk[0]] -= 1
        ppp[kkk[1]] -= 1
        if ppp[kkk[1]] != 0 != 0 and ppp[kkk[0]] != 0 and on[kkk[1]][ppp[kkk[1]]] == kkk[0] and on[kkk[0]][ppp[kkk[0]]] == kkk[1]:
            de.append([on[kkk[1]][ppp[kkk[1]]],kkk[1]])
        else:
            if len(on[kkk[1]]) != 0:
                de.append([on[kkk[1]][ppp[kkk[1]]],kkk[1]])
            if len(on[kkk[0]]) != 0:
                de.append([on[kkk[0]][ppp[kkk[0]]],kkk[0]])


print(co)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = [list(map(int,input().split())) for _ in range(n)]

total = [0]*n

i = 0
count = 0
day = 0
while count < (n*(n-1))//2:
    while total[i] < n-1:
        game = [0] * n
        flag = False
        for j in range(i,n):
            if total[j] == n-1:
                break
            tmp = a[j][total[j]]
            if game[j] == 0 and game[tmp-1] == 0 and a[tmp-1][total[tmp-1]] == j+1:
                total[j] += 1
                total[tmp-1] += 1
                game[j] += 1
                game[tmp-1] += 1
                count += 1
                flag = True
        if flag == False:
            break
        day += 1
    if flag == False:
        break
    i += 1

if flag == False:
    print(-1)
else:
    print(day)

Submission status: Wrong Answer

Solution code:
for i in range(int(input())):
    print(10)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
from queue import Queue
n = int(input())
a = [list(map(lambda x: int(x) - 1, input().split())) for _ in range(n)]

nxt = {}
for i in range(n):
    for j in range(n - 2):
        l = i
        r = a[i][j]
        if l > r:
            l, r = r, l
        s = (l, r)
        l = i
        r = a[i][j + 1]
        if l > r:
            l, r = r, l
        t = (l, r)
        nxt[s] = nxt[s] + [t] if s in nxt else [t]

deg = {}
for ts in nxt.values():
    for t in ts:
        deg[t] = deg[t] + 1 if t in deg else 1

q = Queue()
d = {}
for s in nxt.keys():
    if s not in deg:
        d[s] = 1
        q.put(s)
if q.empty():
    print(-1)
    exit()

ans = 1
while not q.empty():
    s = q.get()
    if d[s] == -1:
        print(-1)
        exit()
    if s not in nxt:
        continue
    for t in nxt[s]:
        d[t] = max(d[t], d[s] + 1) if t in d else d[s] + 1
        ans = max(ans, d[t])
        deg[t] -= 1
        if deg[t] == 0:
            q.put(t)
    d[s] = -1

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
u = [0]*n
d = 0
preunc = -1
unc = 0
allgall = False
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
    allgall = True
    for w in range(n):
        if u[w] != n-1:
            allgall = False
    if allgall:
        print(max(u)+1)
        break
    if unc!=0:
        if preunc == unc:
            print(-1)
            break
        else:
            preunc = unc
    else:preunc = 0
    d+=1
    if d>499500: #(1000*999/2)
        print(-1)
        break

Submission status: Wrong Answer

Solution code:
n = int(input())
matches = [[int(v) - 1 for v in input().split()] for _ in range(n)]

class Node:
    def __init__(self, pl1, pl2):
        self.pl1 = pl1
        self.pl2 = pl2
        self.next = []

def swap(s, t):
    if s < t:
        return s, t
    else:
        return t, s

table = {}
for p in range(n):
    for q in range(p+1, n):
        table[p, q] = Node(p, q)

starts = set()
for player in range(n):
    before = table[swap(player, matches[player][0])]
    starts.add(before)
    for opponent in matches[player][1:]:
        nex = table[swap(player, opponent)]
        before.next.append(nex)
        before = nex


def dfs(start):
    stack = []
    used = set()
    stack.append(start)
    used.add(start)
    res = -1
    while len(stack) > 0:
        node = stack.pop()
        if len(node.next) == 0:
            res = max(res, len(used))
        else:
            for nx in node.next:
                if nx not in used:
                    used.add(nx)
                    stack.append(nx)
                    break
    return res

result = 0
for start in starts:
    temp = dfs(start)
    if temp < 0:
        result = -1
        break
    result = max(result, temp)

print(result)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
from queue import Queue
n = int(input())
a = [list(map(lambda x: int(x) - 1, input().split())) for _ in range(n)]

nxt = {}
for i in range(n):
    for j in range(n - 2):
        l = i
        r = a[i][j]
        if l > r:
            l, r = r, l
        s = (l, r)
        l = i
        r = a[i][j + 1]
        if l > r:
            l, r = r, l
        t = (l, r)
        nxt[s] = nxt[s] + [t] if s in nxt else [t]

deg = {}
for ts in nxt.values():
    for t in ts:
        deg[t] = deg[t] + 1 if t in deg else 1

q = Queue()
d = {}
for s in nxt.keys():
    if s not in deg:
        d[s] = 1
        q.put(s)
if q.empty():
    print(-1)
    exit()

ans = 1
while not q.empty():
    s = q.get()
    if s not in nxt:
        continue
    for t in nxt[s]:
        d[t] = max(d[t], d[s] + 1) if t in d else d[s] + 1
        ans = max(ans, d[t])
        print(s, t, d[t])
        deg[t] -= 1
        if deg[t] == 0:
            q.put(t)

print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = []
for i in range(N):
    a = list(map(int, input().split(" ")))
    A.append(a)

for day in range(N * (N-1) // 2):
    #print("Day {}".format(day + 1))
    #print(A)
    #print("==")
    m = 0
    skip = []
    for i in range(N):
        if not (i in skip) and (len(A[i]) > 0):
            opp = A[i][0]
            if (len(A[opp-1]) > 0) and not ((opp-1) in skip):
                if A[opp - 1][0] == (i + 1):
                    A[i] = A[i][1:]
                    A[opp - 1] = A[opp - 1][1:]
                    # print("{} vs {}".format(i+1,opp))
                    skip.append(i)
                    skip.append(opp-1)
                    m = m + 1
    #print("{} matches played".format(m))
    if (m == 0):
        for i in range(N):
            if (len(A[i]) > 0):
                print(-1)
                exit()
        break
print(day)

Submission status: Wrong Answer

Solution code:
from collections import deque
import time
st = time.time()
N = int(input())
dic = {}
for i in range(N):
  dic[i+1] = deque([int(i) for i in input().split()])
 
ans = 0
allpc = 0
stflag = False
while (True):
  if time.time()-st > 1.5:
    stflag = True
    break
  tmpset = set()
  playcount = 0
  for i in range(1, N+1):
    if i not in tmpset and len(dic[i]) > 0:
      aite = dic[i].popleft()
      if aite not in tmpset:
        aiteaite = dic[aite].popleft()
        if aiteaite == i:
          tmpset.add(i)
          tmpset.add(aite)
          playcount += 1
          allpc += 1
        else:
          dic[i].appendleft(aite)
          dic[aite].appendleft(aiteaite)
      else:
        dic[i].appendleft(aite)
 
  if playcount > 0:
    ans += 1
  else:
    break
if stflag:
  print(N * (N-1)//2)
if allpc != int(N * (N-1) /2):
  print(-1)
else:
  print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque
import time
st = time.time()
N = int(input())
dic = {}
for i in range(N):
  dic[i+1] = deque([int(i) for i in input().split()])
 
ans = 0
allpc = 0
stflag = False
while (True):
  if time.time()-st > 1.5:
    stflag = True
    break
  tmpset = set()
  playcount = 0
  for i in range(1, N+1):
    if i not in tmpset and len(dic[i]) > 0:
      aite = dic[i].popleft()
      if aite not in tmpset:
        aiteaite = dic[aite].popleft()
        if aiteaite == i:
          tmpset.add(i)
          tmpset.add(aite)
          playcount += 1
          allpc += 1
        else:
          dic[i].appendleft(aite)
          dic[aite].appendleft(aiteaite)
      else:
        dic[i].appendleft(aite)
 
  if playcount > 0:
    ans += 1
  else:
    break
if stflag:
  print(int(N * (N-1))//2)
if allpc != int(N * (N-1) /2):
  print(-1)
else:
  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
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(0)
    if (unc!=0 and preunc == unc):
        print(-1)
        break
    else:
        if unc != 0:
            preunc = unc
        else:
            preunc = 0
    d+=1
    if d>499500: #(1000*999/2)
        print(-1)
        break

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
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(0)
    if preunc == unc:
        print(-1)
        break
    else:
        if unc != 0:
            preunc = unc
        else:
            preunc = 0
    d+=1
    if d>499500: #(1000*999/2)
        print(-1)
        break

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**7)
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def SI(): return sys.stdin.readline().strip()
INF = 10 ** 18
MOD = 10 ** 9 + 7



def main(): 
    N = II()
    A = [[] for _ in range(N)]
    for i in range(N):
        A[i] = LI()

    # print(A)
    # return

    from collections import defaultdict
    outs = defaultdict(list)
    ins = defaultdict(int)
    for i in range(N):
        # prev_v = (i+1, A[i][0]) if i+1 < A[i][0] else (A[i][0], i+1)
        for j in range(1,N-1):
            prev_v = (i+1, A[i][j-1]) if i+1 < A[i][j-1] else (A[i][j-1], i+1)
            next_v = (i+1, A[i][j]) if (i+1 < A[i][j]) else (A[i][j] , i+1)
            outs[prev_v].append(next_v)
            ins[next_v] += 1

    # print(outs, ins)
    # return

    from collections import deque
    q = deque()
    for i in range(1, N+1):
        j = A[i-1][0]
        if i < j and ins[(i, j)] == 0:
            q.append([(i, j), 0])

    if not q:
        print(-1)
        return

    # q = deque(v1 for v1 in range(1, N+1) if ins[v1] == 0)
    res = []
    depth_max = 0
    while q:
        v1, depth = q.popleft()
        res.append(v1)
        for v2 in outs[v1]:
            ins[v2] -= 1
            if ins[v2] == 0:
                q.append([v2, depth+1])
                depth_max = max(depth_max, depth+1)
    if q:
        print(-1)
        return
    else:
        print(depth_max+1)
        return

main()

Submission status: Wrong Answer

Solution code:

import collections as cl
def l(): #intのlist
    return list(map(int,input().split()))
def onem(): #Nとかの取得
    return int(input())
n = onem()

on = []
de = cl.deque()
ppp = [n-1 for i in range(n)]
for i in range(n):
    k = l()
    for j in range(n-1):
        k[j] -= 1
    k.reverse()
    de.append([k[0],i])
    on.append(cl.deque(k))


co = 1
ddd = cl.deque()
de.append([-1,-1])
cl = [1 for i in range(n)]
for aaa in range(n**2):
    kkk = de.popleft()
    if kkk[0] == -1 and kkk[1] == -1:
        if sum(cl) == n:
            co = -1
            break
        if len(de) == 0:
            break
        de.append([-1,-1])
        co += 1
        cl = [1 for i in range(n)]

    elif ppp[kkk[0]] != 0 and on[kkk[0]][0] == kkk[1] and cl[kkk[0]] and cl[kkk[1]]:
        cl[kkk[0]] = 0
        cl[kkk[1]] = 0
        on[kkk[0]].popleft()
        on[kkk[1]].popleft()
        ppp[kkk[0]] -= 1
        ppp[kkk[1]] -= 1
        if ppp[kkk[1]] != 0 != 0 and ppp[kkk[0]] != 0 and on[kkk[1]][0] == kkk[0] and on[kkk[0]][0] == kkk[1]:
            de.append([on[kkk[1]][0],kkk[1]])
        else:
            if len(on[kkk[1]]) != 0:
                de.append([on[kkk[1]][0],kkk[1]])
            if len(on[kkk[0]]) != 0:
                de.append([on[kkk[0]][0],kkk[0]])


print(co)

Submission status: Wrong Answer

Solution code:


import collections as cl
def l(): #intのlist
    return list(map(int,input().split()))
def onem(): #Nとかの取得
    return int(input())
n = onem()

on = []
de = cl.deque()
ppp = [n-1 for i in range(n)]
for i in range(n):
    k = l()
    for j in range(n-1):
        k[j] -= 1
    k.reverse()
    de.append([k[0],i])
    on.append(cl.deque(k))


co = 1
ddd = cl.deque()
de.append([-1,-1])
cl = [1 for i in range(n)]
for aaa in range(n*(n-1)):
    kkk = de.popleft()
    if kkk[0] == -1 and kkk[1] == -1:
        if sum(cl) == n:
            co = -1
            break
        if len(de) == 0:
            break
        de.append([-1,-1])
        co += 1
        cl = [1 for i in range(n)]

    elif ppp[kkk[0]] != 0 and on[kkk[0]][0] == kkk[1] and cl[kkk[0]] and cl[kkk[1]]:
        cl[kkk[0]] = 0
        cl[kkk[1]] = 0
        on[kkk[0]].popleft()
        on[kkk[1]].popleft()
        ppp[kkk[0]] -= 1
        ppp[kkk[1]] -= 1
        if ppp[kkk[1]] != 0 != 0 and ppp[kkk[0]] != 0 and on[kkk[1]][0] == kkk[0] and on[kkk[0]][0] == kkk[1]:
            de.append([on[kkk[1]][0],kkk[1]])
        else:
            if len(on[kkk[1]]) != 0:
                de.append([on[kkk[1]][0],kkk[1]])
            if len(on[kkk[0]]) != 0:
                de.append([on[kkk[0]][0],kkk[0]])


print(co)

Submission status: Wrong Answer

Solution code:
import numpy as np
N = input()
N = int(N)

A = []
for i in range(N):
  a = input().split()
  a = np.hstack([int(a[i]) for i in range(N-1)])
  A.append(a)

count = 0
day = 0
matchNum = N * (N-1) // 2
isDone = -1

while(count < matchNum):
  for i in range(N):
    if len(A[i]) > 0:
      i_next = A[i][0] - 1
      if i_next >= 0:
        if A[i_next][0] == i + 1:
          A[i][0] = -100
          A[i_next][0] = -100
          isDone = 1
          count += 1
  
  if isDone == -1:
    print(-1)
    break
  else:
    for i in range(N):
      if len(A[i]) != 0:
        if A[i][0] == -100:
          A[i] = A[i][1:]
    day += 1
    
if isDone:
    print(day)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(2000000)


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

visited = [[-1] *(len(a[0])) for i in range(n)]
memo = [[0] * (n) for i in range(n)]
for i in range(n):
    for j in range(len(a[0])):
        memo[i][a[i][j]-1] = j
        
def solve(i, j, rec_times):
    if rec_times >= n + 10:
        print(-1)
        exit()
    i_me = i
    j_me = j
    i_you = a[i_me][j_me] - 1
    j_you = memo[i_you][i_me]
    if j_me == 0 and j_you == 0:
        visited[i_me][j_me] = 1
        visited[i_you][j_you] = 1
        return 1
    
    if visited[i_me][j_me] > 0 and visited[i_you][j_you] > 0:
        return max(visited[i_me][j_me], visited[i_you][j_you])
 
    if j_you == 0:
        day = solve(i_me, j_me - 1, rec_times + 1) + 1
    elif j_me == 0:
        day = solve(i_you, j_you - 1, rec_times + 1) + 1    
    else:
        day = max(solve(i_me, j_me - 1, rec_times + 1), solve(i_you, j_you - 1, rec_times + 1)) + 1
    visited[i_me][j_me] = day
    visited[i_you][j_you] = day
    return day
    
for i in range(n):
    solve(i, len(a[0]) - 1, 0)
ans = 0
for i in range(n):
    ans = max(ans,visited[i][-1])
print(ans)

Submission status: Wrong Answer

Solution code:
def make_tree(index, a_list):
    depth = 1
    for a in a_list:
        depth = max(tree[a, index], depth)
        tree[a, index] = tree[index, a] = depth
        depth += 1

import collections

tree = collections.Counter()

N = int(input())
for n in range(N):
    a_list = list(map(int, input().split()))
    make_tree(n + 1, a_list)
    
#if(tree.most_common()[-1][1] != 1):
#    print(-1)
#else:
#    print(tree.most_common()[0][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
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(0)
    if preunc == unc:
        print(-1)
        break
    else:
        if unc != 0:
            preunc = unc
    d+=1
    if d>499500: #(1000*999/2)
        print(-1)
        break

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)]
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)
finish = [False for i in range(M)]
candidate = [(i, 0) for i in range(M) if len(parent[i]) == 0]
depth = [-1 for i in range(M)]
longest = 0

while len(candidate) > 0:
	x = candidate.pop()
	c,d = x[0], x[1]
	e = None
	del_list = []
	for e in edge[c]:
		parent[e].remove(c)
		if len(parent[e]) == 0:
			candidate.append((e, d+1))
			del_list.append(e)
	if e == None:
		longest = max(longest, d)
	for e in del_list:
		edge[c].remove(e)
all_finished = sum([sum(parent[i]) for i in range(M)]) == 0
if all_finished:
	print(longest + 1)
else:
	print(-1)

Submission status: Wrong Answer

Solution code:



import collections as cl
def l(): #intのlist
    return list(map(int,input().split()))
def onem(): #Nとかの取得
    return int(input())
n = onem()

on = []
de = cl.deque()
ppp = [n-1 for i in range(n)]
for i in range(n):
    k = l()
    for j in range(n-1):
        k[j] -= 1
    k.reverse()
    de.append([k[0],i])
    on.append(cl.deque(k))


co = 1
ddd = cl.deque()
de.append([-1,-1])
cl = [1 for i in range(n)]
for aaa in range(n*(n-1)//2+1):
    kkk = de.popleft()
    if kkk[0] == -1 and kkk[1] == -1:
        if sum(cl) == n:
            co = -1
            break
        if len(de) == 0:
            break
        de.append([-1,-1])
        co += 1
        cl = [1 for i in range(n)]

    elif ppp[kkk[0]] != 0 and on[kkk[0]][0] == kkk[1] and cl[kkk[0]] and cl[kkk[1]]:
        cl[kkk[0]] = 0
        cl[kkk[1]] = 0
        on[kkk[0]].popleft()
        on[kkk[1]].popleft()
        ppp[kkk[0]] -= 1
        ppp[kkk[1]] -= 1
        if ppp[kkk[1]] != 0 != 0 and ppp[kkk[0]] != 0 and on[kkk[1]][0] == kkk[0] and on[kkk[0]][0] == kkk[1]:
            de.append([on[kkk[1]][0],kkk[1]])
        else:
            if len(on[kkk[1]]) != 0:
                de.append([on[kkk[1]][0],kkk[1]])
            if len(on[kkk[0]]) != 0:
                de.append([on[kkk[0]][0],kkk[0]])


print(co)

Submission status: Wrong Answer

Solution code:
def make_tree(index, a_list):
    depth = 1
    for a in a_list:
        depth = max(tree[a, index], depth)
        tree[a, index] = tree[index, a] = depth
        depth += 1

import collections

tree = collections.Counter()

N = int(input())
for n in range(N):
    a_list = list(map(int, input().split()))
    make_tree(n + 1, a_list)
    
if(tree.most_common()[-1][1] != 1):
    print(-1)
else:
    print(tree.most_common()[0][1])

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
n = int(input())
league = []
match = {}
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 v in match and match[v] == k:
            tmp.append(v)
    for i in tmp:
        if ll[i-1] < 0:
            cnt += 1
            del match[i]
        else:
            match[i] = league[i-1][ll[i-1]]
            ll[i-1] -= 1
    ans += 1
print(ans-1 if n == cnt else -1)

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+100):
    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
    ans += 1
if ans == 1100:
    print(-1)
    exit()
print(ans-1)

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)]
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)
finish = [False for i in range(M)]
candidate = [(i, 0) for i in range(M) if len(parent[i]) == 0]
depth = [-1 for i in range(M)]
longest = 0

while len(candidate) > 0:
	x = candidate.pop()
	c,d = x[0], x[1]
	finish[c] = True
	e = None
	del_list = []
	for e in edge[c]:
		parent[e].remove(c)
		if len(parent[e]) == 0:
			candidate.append((e, d+1))
			del_list.append(e)
	for e in del_list:
		edge[c].remove(e)
	if e == None:
		longest = max(longest, d)
all_finished = all(finish)
if all_finished:
	print(longest + 1)
else:
	print(-1)

Submission status: Wrong Answer

Solution code:
def make_tree(index, a_list):
    depth = 1
    for a in a_list:
        x, y = min(index, a), max(index, a)
        #depth = max(tree[x, y], depth)
        tree[x, y] = depth
        #depth += 1

import collections

tree = collections.Counter()

N = int(input())
for n in range(N):
    a_list = list(map(int, input().split()))
    make_tree(n + 1, a_list)
    
if(tree.most_common()[-1][1] != 1):
    print(-1)
else:
    print(tree.most_common()[0][1])

Submission status: Wrong Answer

Solution code:
N = int(input())

A = [ [int(i) for i in input().split() ] for j in range(N) ]

for i in range(N):
	A[i].append(-1)
	A[i] = [int(s) for s in A[i]]



# Hはリスト
def search(H):
	list=[]
	counter = 0
	for i in range(N):
		if H.count(i+1) > 0:
			l = H.index(i+1)

			if H[i] == l+1 and l > i:
				list.append((i,l))
				counter += 1

	list.append(counter)
	return list

depth_col = [0]*N

look = []

for _ in range(N):
	look.append(A[_][0])

counter = 0

while min(depth_col) < N-1:
	result = search(look)

	if result[-1] == 0:
		counter = -1
		break
	else:
		lengh_search = len(result)-1
		for j in range(lengh_search):
			x = result[j][0]
			y = result[j][1]
			depth_col[x] += 1
			depth_col[y] += 1

			a = depth_col[x]
			b = depth_col[y]
			look[x] = A[x][a]
			look[y] = A[y][b]

		counter += 1

if counter < 0:
	print(-1)
else:
	print(counter)