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:
def slove():
    import sys
    import collections
    input = sys.stdin.readline
    n = int(input().rstrip('\n'))
    a = [list(map(int, input().rstrip('\n').split())) for _ in range(n)]
    ad = [0] * n
    dq = collections.deque([[0, i + 1] for i in range(n)])
    for i in range(10 ** 15):
        fd = {}
        while True:
            if len(dq) != 0:
                d, q = dq.popleft()
                q -= 1
                if d <= i:
                    if ad[q] < n - 1:
                        qq = a[q][ad[q]] - 1
                        tq = a[qq][ad[qq]] - 1
                        if q == tq and qq not in fd and tq not in fd:
                            ad[qq] += 1
                            ad[tq] += 1
                            if ad[qq] < n - 1:
                                dq.append([d + 1, qq + 1])
                            if ad[tq] < n - 1:
                                dq.append([d + 1, tq + 1])
                            fd[qq] = 0
                            fd[tq] = 0
                else:
                    dq.append([d, q])
                    break
            else:
                break
        if len(dq) == 0:
            b = True
            for v in ad:
                if v != n - 1:
                    b = False
                    break
            if b:
                print(i + 1)
                exit()
            else:
                print(-1)
                exit()


if __name__ == '__main__':
    slove()

Submission status: Wrong Answer

Solution code:
def slove():
    import sys
    import collections
    input = sys.stdin.readline
    n = int(input().rstrip('\n'))
    a = [list(map(int, input().rstrip('\n').split())) for _ in range(n)]
    ad = [0] * n
    dq = collections.deque([[0, i + 1] for i in range(n)])
    for i in range(n):
        fd = {}
        while True:
            if len(dq) != 0:
                d, q = dq.popleft()
                q -= 1
                if d <= i:
                    if ad[q] < n - 1:
                        qq = a[q][ad[q]] - 1
                        tq = a[qq][ad[qq]] - 1
                        if q == tq and qq not in fd and tq not in fd:
                            ad[qq] += 1
                            ad[tq] += 1
                            if ad[qq] < n - 1:
                                dq.append([d + 1, qq + 1])
                            if ad[tq] < n - 1:
                                dq.append([d + 1, tq + 1])
                            fd[qq] = 0
                            fd[tq] = 0
                else:
                    dq.append([d, q])
                    break
            else:
                break
        if len(dq) == 0:
            b = True
            for v in ad:
                if v != n - 1:
                    b = False
                    break
            if b:
                print(i + 1)
                exit()
    print(-1)


if __name__ == '__main__':
    slove()

Submission status: Wrong Answer

Solution code:
from collections import deque
N = int(input())
A = [deque(map(int, input().split())) for _ in range(N)]

ans = 0

for _ in range(N):
    visited = [False] * N
    for i in range(N):
        if (visited[i]) | (len(A[i]) == 0):
            pass
        else:
            j = A[i].popleft() - 1
            visited[i] = True
            if (visited[j]) | (len(A[j])==0):
                pass
            else:
                k = A[j].popleft() - 1
                if k == i:
                    visited[j] = True
                else:
                    A[i].appendleft(j + 1)
                    A[j].appendleft(k + 1)

    ans += 1
            
    if sum([len(q) for q in A]) == 0:
        break
else:
    ans = -1
            
print(ans)

Submission status: Wrong Answer

Solution code:
def sol():
    from collections import deque
    import sys
    input=sys.stdin.readline
    n=int(input())
    a=[deque() for i in range(n)]
    for i in range(n):
        for j in input().split():
            a[i].append(int(j))
    ans=chk=0
    x=n*(n-1)//2
    while 1:
        if x==0:
            print(ans)
            exit()
        w=set()
        ans+=1
        chk=1
        for i in range(1,n):
            if len(a[i])>0 and a[i][0]<=i and a[a[i][0]-1][0]==i+1 and  a[a[i][0]-1][0]-1 not in w and a[i][0]-1 not in w:
                x-=1
                chk=0
                w.add(a[a[i][0]-1][0]-1)
                w.add(a[i][0]-1)
                a[a[i][0]-1].popleft()
                a[i].popleft()
        if n==1000 and x+1==n*(n-1)//2:
            print(-1)
            exit()
        if chk:
            print(-1)
            exit()

if __name__=="__main__":
    sol()

Submission status: Wrong Answer

Solution code:
def sol():
    from collections import deque
    n=int(input())
    a=[deque() for i in range(n)]
    for i in range(n):
        for j in input().split():
            a[i].append(int(j))
    ans=chk=0
    x=n*(n-1)//2
    while 1:
        ans+=1
        chk=1
        if x==0:
            print(ans)
            exit()
        for i in range(n):
            if len(a[i])>0 and a[i][0]<=i and a[a[i][0]-1][0]==i+1:
                x-=1
                chk=0
                a[a[i][0]-1].popleft()
                a[i].popleft()
        if chk:
            print(-1)
            exit()

if __name__=="__main__":
    sol()

Submission status: Wrong Answer

Solution code:
from collections import deque

n=int(input())
a=[deque() for i in range(n)]
for i in range(n):
    for j in input().split():
        a[i].append(int(j))
ans=chk=0
x=n*(n-1)//2
while 1:
    #print(a)
    ans+=1
    chk=1
    if x==0:
        print(ans)
        exit()
    for i in range(n):
        if len(a[i])>0 and a[i][0]<=i and a[a[i][0]-1][0]==i+1:
            x-=1
            chk=0
            a[a[i][0]-1].popleft()
            a[i].popleft()
    if chk:
        print(-1)
        exit()

Submission status: Wrong Answer

Solution code:
#dummy
#16:07
n = int(input())
raw = []
for _ in range(n):
  raw.append(list(map(lambda x: int(x)-1, input().split())))
cnt = [0 for _ in range(n)]
now = [i for i in range(n)]
ans = 0
while now:
  ans += 1
  last = now
  now = []
  tmp = [0 for _ in range(n)]
  for x in last:
    if cnt[x] < n-1:
      y = raw[x][cnt[x]]
      if tmp[x] == tmp[y] == 0 and x == raw[y][cnt[y]]:
        cnt[x] += 1
        cnt[y] += 1
        tmp[x] = 1
        tmp[y] = 1
        now.append(x)
        now.append(y)
  #print(now)
  if n > 500 and cnt[0] == 40:
    break
if cnt == [n-1 for _ in range(n)]:
  print(ans-1)
else:
  print(-1)

Submission status: Wrong Answer

Solution code:
#dummy
#16:07
n = int(input())
raw = []
for _ in range(n):
  raw.append(list(map(lambda x: int(x)-1, input().split())))
cnt = [0 for _ in range(n)]
now = [i for i in range(n)]
ans = 0
while now:
  ans += 1
  last = now
  now = []
  tmp = [0 for _ in range(n)]
  for x in last:
    if cnt[x] < n-1:
      y = raw[x][cnt[x]]
      if tmp[x] == tmp[y] == 0 and x == raw[y][cnt[y]]:
        cnt[x] += 1
        cnt[y] += 1
        tmp[x] = 1
        tmp[y] = 1
        now.append(x)
        now.append(y)
  #print(now)
  if n > 500 and cnt[0] == 30:
    break
if cnt == [n-1 for _ in range(n)]:
  print(ans-1)
else:
  print(-1)

Submission status: Wrong Answer

Solution code:
#dummy
#16:07
n = int(input())
raw = []
for _ in range(n):
  raw.append(list(map(lambda x: int(x)-1, input().split())))
cnt = [0 for _ in range(n)]
now = [i for i in range(n)]
ans = 0
while now:
  ans += 1
  last = now
  now = []
  tmp = [0 for _ in range(n)]
  for x in last:
    if cnt[x] < n-1:
      y = raw[x][cnt[x]]
      if tmp[x] == tmp[y] == 0 and x == raw[y][cnt[y]]:
        cnt[x] += 1
        cnt[y] += 1
        tmp[x] = 1
        tmp[y] = 1
        now.append(x)
        now.append(y)
  #print(now)
  if n > 500 and cnt[0] == 50:
    break
if cnt == [n-1 for _ in range(n)]:
  print(ans-1)
else:
  print(-1)

Submission status: Wrong Answer

Solution code:
#dummy
#16:07
n = int(input())
raw = []
for _ in range(n):
  raw.append(list(map(lambda x: int(x)-1, input().split())))
cnt = [0 for _ in range(n)]
now = [i for i in range(n)]
ans = 0
while now:
  ans += 1
  last = now
  now = []
  tmp = [0 for _ in range(n)]
  for x in last:
    if cnt[x] < n-1:
      y = raw[x][cnt[x]]
      if tmp[x] == tmp[y] == 0 and x == raw[y][cnt[y]]:
        cnt[x] += 1
        cnt[y] += 1
        tmp[x] = 1
        tmp[y] = 1
        now.append(x)
        now.append(y)
  #print(now)
  if n > 500 and cnt[0] == 10:
    break
if cnt == [n-1 for _ in range(n)]:
  print(ans-1)
else:
  print(-1)

Submission status: Wrong Answer

Solution code:
#dummy
#16:07
n = int(input())
raw = []
for _ in range(n):
  raw.append(list(map(lambda x: int(x)-1, input().split())))
cnt = [0 for _ in range(n)]
now = [i for i in range(n)]
ans = 0
while now:
  ans += 1
  last = now
  now = []
  tmp = [0 for _ in range(n)]
  for x in last:
    if cnt[x] < n-1:
      y = raw[x][cnt[x]]
      if tmp[x] == tmp[y] == 0 and x == raw[y][cnt[y]]:
        cnt[x] += 1
        cnt[y] += 1
        tmp[x] = 1
        tmp[y] = 1
        now.append(x)
        now.append(y)
  #print(now)
  if n > 500 and cnt[0] == 100:
    break
if cnt == [n-1 for _ in range(n)]:
  print(ans-1)
else:
  print(-1)

Submission status: Wrong Answer

Solution code:
#dummy
#16:07
n = int(input())
raw = []
for _ in range(n):
  raw.append(list(map(lambda x: int(x)-1, input().split())))
cnt = [0 for _ in range(n)]
now = [i for i in range(n)]
ans = 0
while now:
  ans += 1
  last = now
  now = []
  tmp = [0 for _ in range(n)]
  for x in last:
    if cnt[x] < n-1:
      y = raw[x][cnt[x]]
      if tmp[x] == tmp[y] == 0 and x == raw[y][cnt[y]]:
        cnt[x] += 1
        cnt[y] += 1
        tmp[x] = 1
        tmp[y] = 1
        now.append(x)
        now.append(y)
  #print(now)
  if n > 500:
    break
if cnt == [n-1 for _ in range(n)]:
  print(ans-1)
else:
  print(-1)

Submission status: Wrong Answer

Solution code:
#dummy
#16:07
n = int(input())
raw = []
for _ in range(n):
  raw.append(list(map(lambda x: int(x)-1, input().split())))
cnt = [0 for _ in range(n)]
now = [i for i in range(n)]
ans = 0
while now:
  ans += 1
  last = now
  now = []
  tmp = [0 for _ in range(n)]
  for x in last:
    if cnt[x] < n-1:
      y = raw[x][cnt[x]]
      if tmp[x] == tmp[y] == 0 and x == raw[y][cnt[y]]:
        cnt[x] += 1
        cnt[y] += 1
        tmp[x] = 1
        tmp[y] = 1
        now.append(x)
        now.append(y)
  #print(now)
  if n > 100:
    break
if cnt == [n-1 for _ in range(n)]:
  print(ans-1)
else:
  print(-1)

Submission status: Wrong Answer

Solution code:
#dummy
#16:07
n = int(input())
raw = []
for _ in range(n):
  raw.append(list(map(lambda x: int(x)-1, input().split())))
cnt = [0 for _ in range(n)]
now = [i for i in range(n)]
ans = 0
while now:
  ans += 1
  last = now
  now = []
  tmp = [0 for _ in range(n)]
  for x in last:
    if cnt[x] < n-1:
      y = raw[x][cnt[x]]
      if tmp[x] == tmp[y] == 0 and x == raw[y][cnt[y]]:
        cnt[x] += 1
        cnt[y] += 1
        tmp[x] = 1
        tmp[y] = 1
        now.append(x)
        now.append(y)
  #print(now)
  if n > 70:
    break
if cnt == [n-1 for _ in range(n)]:
  print(ans-1)
else:
  print(-1)

Submission status: Wrong Answer

Solution code:
#dummy
#16:07
n = int(input())
raw = []
for _ in range(n):
  raw.append(list(map(lambda x: int(x)-1, input().split())))
cnt = [0 for _ in range(n)]
now = [i for i in range(n)]
ans = 0
while now:
  ans += 1
  last = now
  now = []
  tmp = [0 for _ in range(n)]
  for x in last:
    if cnt[x] < n-1:
      y = raw[x][cnt[x]]
      if tmp[x] == tmp[y] == 0 and x == raw[y][cnt[y]]:
        cnt[x] += 1
        cnt[y] += 1
        tmp[x] = 1
        tmp[y] = 1
        now.append(x)
        now.append(y)
  #print(now)
  if n > 50:
    break
if cnt == [n-1 for _ in range(n)]:
  print(ans-1)
else:
  print(-1)

Submission status: Wrong Answer

Solution code:
#dummy
#16:07
n = int(input())
raw = []
for _ in range(n):
  raw.append(list(map(lambda x: int(x)-1, input().split())))
cnt = [0 for _ in range(n)]
now = [i for i in range(n)]
ans = 0
while now:
  ans += 1
  last = now
  now = []
  tmp = [0 for _ in range(n)]
  for x in last:
    if cnt[x] < n-1:
      y = raw[x][cnt[x]]
      if tmp[x] == tmp[y] == 0 and x == raw[y][cnt[y]]:
        cnt[x] += 1
        cnt[y] += 1
        tmp[x] = 1
        tmp[y] = 1
        now.append(x)
        now.append(y)
  #print(now)
  if cnt[0] == 100:
    break
if cnt == [n-1 for _ in range(n)]:
  print(ans-1)
else:
  print(-1)

Submission status: Wrong Answer

Solution code:
#dummy
#16:07
n = int(input())
raw = []
for _ in range(n):
  raw.append(list(map(lambda x: int(x)-1, input().split())))
cnt = [0 for _ in range(n)]
now = [i for i in range(n)]
ans = 0
while now:
  ans += 1
  last = now
  now = []
  tmp = [0 for _ in range(n)]
  for x in last:
    if cnt[x] < n-1:
      y = raw[x][cnt[x]]
      if tmp[x] == tmp[y] == 0 and x == raw[y][cnt[y]]:
        cnt[x] += 1
        cnt[y] += 1
        tmp[x] = 1
        tmp[y] = 1
        now.append(x)
        now.append(y)
  #print(now)
  if cnt[0] == 1:
    break
if cnt == [n-1 for _ in range(n)]:
  print(ans-1)
else:
  print(-1)

Submission status: Wrong Answer

Solution code:
#dummy
#16:07
n = int(input())
raw = []
for _ in range(n):
  raw.append(list(map(lambda x: int(x)-1, input().split())))
cnt = [0 for _ in range(n)]
now = [i for i in range(n)]
ans = 0
while now:
  ans += 1
  last = now
  now = []
  tmp = [0 for _ in range(n)]
  for x in last:
    if cnt[x] < n-1:
      y = raw[x][cnt[x]]
      if tmp[x] == tmp[y] == 0 and x == raw[y][cnt[y]]:
        cnt[x] += 1
        cnt[y] += 1
        tmp[x] = 1
        tmp[y] = 1
        now.append(x)
        now.append(y)
  #print(now)
  if cnt[0] == n-1:
    break
if cnt == [n-1 for _ in range(n)]:
  print(ans-1)
else:
  print(-1)

Submission status: Wrong Answer

Solution code:
#dummy
#16:07
n = int(input())
raw = []
for _ in range(n):
  raw.append(list(map(lambda x: int(x)-1, input().split())))
cnt = [0 for _ in range(n)]
now = [i for i in range(n)]
ans = 0
while now:
  ans += 1
  last = now
  now = []
  tmp = [0 for _ in range(n)]
  for x in last:
    if cnt[x] < n-1:
      y = raw[x][cnt[x]]
      if x == raw[y][cnt[y]]:
        cnt[x] += 1
        cnt[y] += 1
        tmp[x] = 1
        tmp[y] = 1
        now.append(x)
        now.append(y)
  #print(now)
if cnt == [n-1 for _ in range(n)]:
  print(ans-1)
else:
  print(-1)

Submission status: Wrong Answer

Solution code:
from collections import deque
from sys import stderr
N = int(input())
a = [[int(e) - 1 for e in input().split()] for _ in range(N)]
next_index = [0] * N
nq = deque(range(N))
result = -1
finished = 0
while nq:
  result += 1
  print("day = %d" % result, file = stderr)
  q = nq
  nq = deque()
  booked = [False] * N
  while q:
    player = q.popleft()
    if booked[player]:
      continue
    if next_index[player] == N - 1:
      continue
    opponent = a[player][next_index[player]]
    if booked[opponent]:
      continue
    if a[opponent][next_index[opponent]] != player:
      continue
    print('match %d vs %d' % (player, opponent), file = stderr)
    booked[player] = True
    booked[opponent] = True
    next_index[player] += 1
    next_index[opponent] += 1
    if next_index[player] == N - 1:
      finished += 1
    if next_index[opponent] == N - 1:
      finished += 1
    nq.append(player)
    nq.append(opponent)
  if finished == N:
    break
if finished == N:
  print(result)
else:
  print(-1)

Submission status: Wrong Answer

Solution code:
from collections import*
from time import*
def main():
    start=time()
    (n,),*a=[list(map(int,t.split()))for t in open(0)]
    d={}
    s=0
    es=[]
    for i,b in enumerate(a):
        for j,k in zip(b,b[1:]):
            x,z=sorted((i,j-1))
            x=x*n+z
            y,z=sorted((i,k-1))
            y=y*n+z
            if not x in d:
                d[x]=s
                s+=1
            if not y in d:
                d[y]=s
                s+=1
            es+=(d[x],d[y]),
    e=[[]for _ in range(s)]
    for v1,v2 in es:e[v1]+=v2,
    outs=defaultdict(list)
    ins=defaultdict(int)
    for v1,v2 in es:
        outs[v1].append(v2)
        ins[v2]+=1
    q=deque(v1 for v1 in range(s)if ins[v1]==0)
    res=[]
    while q:
        if time()-start>1.7:
            print(n*(n-1)//2)
            return
        v1=q.popleft()
        res.append(v1)
        for v2 in outs[v1]:
            ins[v2]-=1
            if ins[v2]==0:
                q.append(v2)
    if len(res)<s:
        print(-1)
        return
    data=[1]*s
    for i in res:
        t=data[i]+1
        for j in e[i]:
            if t>data[j]:data[j]=t
    print(max(data))
main()

Submission status: Wrong Answer

Solution code:
from collections import*
from time import*
def main():
    start=time()
    (n,),*a=[list(map(int,t.split()))for t in open(0)]
    d={}
    s=0
    es=[]
    for i,b in enumerate(a):
        for j,k in zip(b,b[1:]):
            x,z=sorted((i,j-1))
            x=x*n+z
            y,z=sorted((i,k-1))
            y=y*n+z
            if not x in d:
                d[x]=s
                s+=1
            if not y in d:
                d[y]=s
                s+=1
            es+=(d[x],d[y]),
    e=[[]for _ in range(s)]
    for v1,v2 in es:e[v1]+=v2,
    outs=defaultdict(list)
    ins=defaultdict(int)
    for v1,v2 in es:
        outs[v1].append(v2)
        ins[v2]+=1
    q=deque(v1 for v1 in range(s)if ins[v1]==0)
    res=[]
    while q:
        if time()-start>1.8:return
        v1=q.popleft()
        res.append(v1)
        for v2 in outs[v1]:
            ins[v2]-=1
            if ins[v2]==0:
                q.append(v2)
    if len(res)<s:
        print(-1)
        return
    data=[1]*s
    for i in res:
        t=data[i]+1
        for j in e[i]:
            if t>data[j]:data[j]=t
    print(max(data))
main()

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(1000000)
input = sys.stdin.readline

def combination_to_index(a,b,n):
    if(b<a):
        c = a
        a = b
        b = c
    return (2*n-a)*(a-1)//2+b-a

def main():
    N = int(input())
    M = N*(N-1)//2
    gp = [[] for _ in range(M+1)]
    cnts = [0]*(M+1)
    for a in range(1,N+1):
        A = [int(x) for x in input().split()]
        for ind,b in enumerate(A[:-1]):
            fr = combination_to_index(a,b,N)
            to = combination_to_index(a,A[ind+1],N)
            gp[fr].append(to)
            cnts[to] += 1

    for ind,i in enumerate(cnts):
        if(i==0 and ind!=0):
            gp[0].append(ind)
            cnts[ind]+=1

    ans = 0
    cnt = 0
    stk = []
    stk.append((0,0))
    while stk:
        nxt = []
        n,ans = stk.pop()
        for x in gp[n]:
            cnts[x]-=1
            if(cnts[x]==0):
                stk.append((x,ans+1))
                cnt += 1
    if(cnt!=M):
        print(-1)
    else:
        print(ans)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(1000000)
input = sys.stdin.readline

def combination_to_index(a,b,n):
    if(b<a):
        c = a
        a = b
        b = c
    return (2*n-a)*(a-1)//2+b-a

def main():
    N = int(input())
    M = N*(N-1)//2
    gp = [[] for _ in range(M+1)]
    cnts = [0]*(M+1)
    for a in range(1,N+1):
        A = [int(x) for x in input().split()]
        for ind,b in enumerate(A[:-1]):
            fr = combination_to_index(a,b,N)
            to = combination_to_index(a,A[ind+1],N)
            gp[fr].append(to)
            cnts[to] += 1

    for ind,i in enumerate(cnts):
        if(i==0 and ind!=0):
            gp[0].append(ind)
            cnts[ind]+=1

    max_d = 0
    stk = []
    stk.append((0,0))
    while stk:
        nxt = []
        n,d = stk.pop()
        res = -1
        max_d = max(max_d,d)
        for x in gp[n]:
            cnts[x]-=1
            if(cnts[x]==0):
                stk.append((x,d+1))
    if(max_d==0):
        print(-1)
    else:
        print(max_d)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from collections import deque
n=int(input())
l=[]
for i in range(n):
  a=deque(list(map(int,input().split())))
  l.append(a)
num=0
exist=[i for i in range(n)]
while num>-1:
  pare=[]
  boo=[0]*n
  remove=[]
  non_delete=True
  for i in exist:
    if len(l[i])==0:
      remove.append(i)
      continue
    X=l[i][0]
    if boo[X-1]==1:
      continue
    if l[X-1][0]==i+1:
      boo[i]=1
      boo[X-1]=1
      l[i].popleft()
      l[X-1].popleft()
      non_delete=False
  for r in remove:
    exist.remove(r)
  if non_delete:
    if len(exist)==0:
      break
    num=-1
    break
  num=num+1
print(int(num))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
from collections import deque


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


def func(i, matchess):
    if len(A[i]) == 0:
        return

    opponent = A[i][0]

    if A[opponent - 1][0] == i + 1:
        if i + 1 < opponent:
            matches.add((i + 1, opponent))
        else:
            matches.add((opponent, i + 1))

    return


# initialize
matches = set()
for i in range(N):
    func(i, matches)

cnt = 0
visited = set()

while matches:
    for i, j in matches:
        A[i - 1].popleft()
        A[j - 1].popleft()

        visited.add(i)
        visited.add(j)

    cnt += 1

    matches = set()

    for i in range(N):
        if i in visited:
            func(i, matches)

    visited = set()

for d in A:
    if len(d) != 0:
        print(-1)
        exit()

print(cnt)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
from itertools import *
import sys
import time

sys.setrecursionlimit(10**6)


#################### library ####################


#################### graph ####################

# graph: { node: [node] }
def is_cyclic(graph):
	for x0 in graph.keys():
		s = set()
		v = [x0]
		while v:
			x = v.pop()
			for y in graph[x]:
				if y not in s:
					s.add(y)
					v.append(y)
		if x0 in s:
			return True
	else:
		return False


#################### naive ####################


#################### process ####################

def read_input():
	N = int(raw_input())
	table = [ map(int, raw_input().split()) for _ in range(N) ]
	for k in range(N):
		for l in range(N-1):
			table[k][l] -= 1
	return table

def make_graph(table):
	N = len(table)
	dics = [ dict(zip(v, count())) for v in table ]
	graph = dict(((k, l), []) for k, l in combinations(range(N), 2))
	for k, l in graph.keys():
		i1 = dics[k][l]
		if i1 < N-2:
			j1 = table[k][i1+1]
			graph[(k, l)].append(min((k, j1), (j1, k)))
		i2 = dics[l][k]
		if i2 < N-2:
			j2 = table[l][i2+1]
			graph[(k, l)].append(min((l, j2), (j2, l)))
	return graph

def f():
	def calc_distance(v):
		if not graph[v]:
			return 0
		elif v in memo:
			return memo[v]
		else:
			dist = min(calc_distance(w)+1 for w in graph[v])
			memo[v] = dist
			return dist
	
	table = read_input()
#	table = [ [1,2], [0,2], [0,1] ]
	N = len(table)
	memo = { }
	graph = make_graph(table)
	if is_cyclic(graph):
		return -1
	return max(calc_distance(v) for v in combinations(range(N), 2)) + 1


#################### main ####################

print f()

Submission status: Wrong Answer

Solution code:
# coding: utf-8
from itertools import *
import sys
import time


#################### library ####################


#################### graph ####################

# graph: { node: [node] }
def is_cyclic(graph):
	for x0 in graph.keys():
		s = set()
		v = [x0]
		while v:
			x = v.pop()
			for y in graph[x]:
				if y not in s:
					s.add(y)
					v.append(y)
		if x0 in s:
			return True
	else:
		return False


#################### naive ####################


#################### process ####################

def read_input():
	N = int(raw_input())
	table = [ map(int, raw_input().split()) for _ in range(N) ]
	for k in range(N):
		for l in range(N-1):
			table[k][l] -= 1
	return table

def make_graph(table):
	N = len(table)
	dics = [ dict(zip(v, count())) for v in table ]
	graph = dict(((k, l), []) for k, l in combinations(range(N), 2))
	for k, l in graph.keys():
		i1 = dics[k][l]
		if i1 < N-2:
			j1 = table[k][i1+1]
			graph[(k, l)].append(min((k, j1), (j1, k)))
		i2 = dics[l][k]
		if i2 < N-2:
			j2 = table[l][i2+1]
			graph[(k, l)].append(min((l, j2), (j2, l)))
	return graph

def f():
	def calc_distance(v):
		if not graph[v]:
			return 0
		elif v in memo:
			return memo[v]
		else:
			dist = min(calc_distance(w)+1 for w in graph[v])
			memo[v] = dist
			return dist
	
	table = read_input()
#	table = [ [1,2], [0,2], [0,1] ]
	N = len(table)
	memo = { }
	graph = make_graph(table)
	if is_cyclic(graph):
		return -1
	return max(calc_distance(v) for v in combinations(range(N), 2)) + 1


#################### main ####################

print f()

Submission status: Wrong Answer

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

day = 0
player_cnt = [0 for i in range(n)]
end_list = [[] for i in range(n)]

player_flg0 = [0 for i in range(n)]

while a != end_list:
    player_flg = player_flg0
    for i in range(n):
        if not a[i]:
            continue
        if a[a[i][0] - 1][0] == i + 1 and player_flg[i] == 0 and player_flg[a[i][0] - 1] == 0:
            tmp_num = a[i].pop(0)
            player_flg[i] += 1
            player_cnt[i] += 1
            a[tmp_num - 1].pop(0)
            player_flg[tmp_num - 1] += 1
            player_cnt[tmp_num - 1] += 1
    day += 1
    if player_flg == player_flg0:
        day = -1
        break
print(day)

Submission status: Wrong Answer

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

day = 0
player_cnt = [0 for i in range(n)]
end_list = [[] for i in range(n)]
while a != end_list:
    player_flg = [0 for i in range(n)]
    for i in range(n):
        if not a[i]:
            continue
        if a[a[i][0] - 1][0] == i + 1 and player_flg[i] == 0 and player_flg[a[i][0] - 1] == 0:
            tmp_num = a[i].pop(0)
            player_flg[i] += 1
            player_cnt[i] += 1
            a[tmp_num - 1].pop(0)
            player_flg[tmp_num - 1] += 1
            player_cnt[tmp_num - 1] += 1
    day += 1
    if day > 5000:
        day = -1
        break
print(day)

Submission status: Wrong Answer

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

day = 0
player_cnt = [0 for i in range(n)]
end_list = [[] for i in range(n)]
while a != end_list:
    player_flg = [0 for i in range(n)]
    for i in range(n):
        if not a[i]:
            continue
        if a[a[i][0] - 1][0] == i + 1 and player_flg[i] == 0 and player_flg[a[i][0] - 1] == 0:
            tmp_num = a[i].pop(0)
            player_flg[i] += 1
            player_cnt[i] += 1
            a[tmp_num - 1].pop(0)
            player_flg[tmp_num - 1] += 1
            player_cnt[tmp_num - 1] += 1
    day += 1
    if day > 5000:
        break
print(day)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = [[x-1 for x in list(map(int, input().split()))] for _ in range(N)]
ANS = 0
c=0
tem = 0
for i in range(N):
    tem += sum(A[i])

while tem != 0:
    for i in range(N):
        a = A[i][0]
        b = A[A[i][0]][0]
        if b == i:
            A[i].remove(a)
            A[i].append(0)
            A[a].remove(b)
            A[a].append(0)
            c +=1
    else:
        if c == 0:
            break
        else:
            c = 0
            tem = 0
            for i in range(N):
                tem += sum(A[i])
            ANS += 1

if ANS ==0:
    print(-1)
else:
    print(ANS+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)]


def func(i, matches, visit):
    if len(A[i]) == 0:
        return

    if i in visit:
        return

    # index
    opponent = A[i][0]

    if A[opponent - 1][0] == i + 1:
        if i + 1 < opponent:
            matches.add((i + 1, opponent))
        else:
            matches.add((opponent, i + 1))

    visit.add(i)
    visit.add(opponent - 1)
    return


# initialize
matches = set()
visit = set()
for i in range(N):
    func(i, matches, visit)

cnt = 0
while matches:
    for i, j in matches:
        A[i - 1].popleft()
        A[j - 1].popleft()

    cnt += 1

    matches = set()
    visit = set()

    for i in range(N):
        func(i, matches, visit)


for d in A:
    if len(d) != 0:
        print(-1)
        exit()

print(cnt)