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 time
limit_time = time.time() +1.7

import sys
input = sys.stdin.readline
N = int(input())
A = [list(map(int, input().split())) for _ in range(N)]

total = N*(N-1)//2
progress = [0]*N
battled = [False]*N
any_battle = False
day = 1
finished = 0
battle_count = 0
for _ in range(total):
    for man in range(N):
        if progress[man] >= N-1 or battled[man]:
            continue
        rival = A[man][progress[man]] - 1
        if battled[rival]:
            continue
        if A[rival][progress[rival]] - 1 != man:
            continue
        progress[man] += 1
        progress[rival] += 1
        battled[man], battled[rival] = True, True
        any_battle = True
        if progress[man] == N-2:
            finished += 1
        if progress[rival] == N-2:
            finished += 1
        battle_count += 1
        if battle_count == total:
            break
    battled = [False]*N
    day += 1
    if finished == N:
        print(day)
        break
    if not any_battle:
        print(-1)
        break
    if limit_time < time.time():
        print(-1)
        break
else:
    print(-1)

Submission status: Wrong Answer

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

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

f = True
day = 1
while(f):
  if day > 1000:
    print(-1)
    sys.exit()
  done = set()
  nokori_games = 0
  
  for m in range(n):
    la = len(a[m])
    nokori_games += la
    if m in done:
      pass
    elif la:
      teki = a[m][0]
      jibun = m + 1
      if a[teki - 1][0] == jibun and teki - 1 not in done:
        a[m].popleft()
        a[teki - 1].popleft()
        done.add(m)
        done.add(teki - 1)
        nokori_games -= 1
      elif not len(a[teki - 1]):
        print(-1)
        sys.exit()
  if nokori_games:
    if not len(done):
      print(-1)
      sys.exit()
  else:
    print(day)
    sys.exit()
  day += 1

Submission status: Wrong Answer

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

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

f = True
day = 1
while(f):
  if day > 200:
    print(-1)
    sys.exit()
  done = set()
  nokori_games = 0
  
  for m in range(n):
    la = len(a[m])
    nokori_games += la
    if m in done:
      pass
    elif la:
      teki = a[m][0]
      jibun = m + 1
      if a[teki - 1][0] == jibun and teki - 1 not in done:
        a[m].popleft()
        a[teki - 1].popleft()
        done.add(m)
        done.add(teki - 1)
        nokori_games -= 1
      elif not len(a[teki - 1]):
        print(-1)
        sys.exit()
  if nokori_games:
    if not len(done):
      print(-1)
      sys.exit()
  else:
    print(day)
    sys.exit()
  day += 1

Submission status: Wrong Answer

Solution code:
N=int(input())


d={}
cnt=0
for i in range(1,N):
	for j in range(i+1, N+1):
		d[(i,j)]=cnt
		d[(j,i)]=cnt
		cnt+=1

L=[]
for i in range(1, N+1):
	t=list(map(int,input().split()))
	for j in range(N-2):
		L.append((d[(i,t[j])], d[i,t[j+1]]))

def func(L):
	t=[0]*(N*(N-1)//2)
	for l in L:
		t[l[1]]+=-1
	
	r=[]
	for i,j in enumerate(t):
		if j==0:r.append(i)
	
	return r

def func2(L, N, cnt):
	
	if L==[]:
		return cnt
	
	r=func(L)
	if r==[]:
		return -1
	
	for l in L:
		if l[0] in r:
			L.remove(l)
	
	return func2(L, N, cnt+1)

print(func2(L,N,0))

Submission status: Wrong Answer

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

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

f = True
day = 1
while(f):
  done = set()
  nokori_games = 0
  
  for m in range(n):
    nokori_games += len(a[m])
    if m in done:
      pass
    elif len(a[m]):
      teki = a[m][0]
      jibun = m + 1
      if not len(a[teki - 1]):
        print(-1)
        sys.exit()
      elif a[teki - 1][0] == jibun and teki - 1 not in done:
        a[m].popleft()
        a[teki - 1].popleft()
        done.add(m)
        done.add(teki - 1)
        nokori_games -= 2
  if not nokori_games:
    print(day)
    sys.exit()
  else:
    if not len(done):
      print(-1)
      sys.exit()
  day += 1

Submission status: Wrong Answer

Solution code:
import sys

N = int(input())
A = []
for i in range(N):
    Ai = [int(v) - 1 for v in input().split()]
    A.append(Ai)

graph = [[[0, []] for i1 in range(N)] for i0 in range(N)]
for i in range(N):
    for j in range(len(A[i]) - 1):
        i0, i1 = i, A[i][j]
        if i0 > i1:
            i1, i0 = i0, i1
        o0, o1 = i, A[i][j + 1]
        if o0 > o1:
            o0, o1 = o1, o0

        graph[i0][i1][1].append((o0, o1))
        graph[o0][o1][0] += 1

queue = []
for i0 in range(N):
    for i1 in range(i0+1, N):
        if graph[i0][i1][0] == 0:
            queue.append((i0, i1))

order = []
while len(queue) > 0:
    i0, i1 = queue.pop()
    order.append((i0, i1))
    for o0, o1 in graph[i0][i1][1]:
        graph[o0][o1][0] -= 1
        if graph[o0][o1][0] == 0:
            queue.append((o0, o1))
if len(order) < N * (N - 1) // 2:
    print(-1)
    sys.exit()

cnt = 1
rest = set()
for i0, i1 in order:
    if i0 in rest or i1 in rest:
        cnt += 1
        rest = set()
    rest.add(i0)
    rest.add(i1)
print(cnt)

Submission status: Wrong Answer

Solution code:
def main():
    N = int(input())
    # A = [list(map(int,input().split())) for _ in range(N)]
    A = []
    next_pos = []
    Apee = A.append
    neapp = next_pos.append
    for _ in range(N):
        npos, *l = list(map(int,input().split()))
        neapp(npos)
        Apee(l)
    # next_pos = [A[i].pop(0) for i in range(N)]
    ans = 0
    # check = [0 for _ in range(N)]
    check = [-1] * N
    while True:
        print(A)
        print(next_pos)
        end = True
        already = set()
        al_add = already.add
        for i in range(N):
            now = next_pos[i] - 1
            if now < 0:
                continue
            if now in already:
                continue
            if i == now:
            	continue
            if i in already:
                continue

            if i == next_pos[now] - 1:
                if end:
                    end = False
                if A[i]:
                    next_pos[i] = A[i].pop(0)
                else:
                    next_pos[i] = -1
                if A[now]:
                    next_pos[now] = A[now].pop(0)
                else:
                    next_pos[now] = -1
                al_add(i)
                al_add(now)
        ans += 1
        if end:
            ans = -1
            break
        if next_pos == check:
            break
    print(ans)
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def main():
    N = int(input())
    # A = [list(map(int,input().split())) for _ in range(N)]
    A = []
    next_pos = []
    Apee = A.append
    neapp = next_pos.append
    for _ in range(N):
        npos, *l = list(map(int,input().split()))
        neapp(npos)
        Apee(l)
    # next_pos = [A[i].pop(0) for i in range(N)]
    ans = 0
    # check = [0 for _ in range(N)]
    check = [-1] * N
    while True:
        print(A)
        print(next_pos)
        end = True
        already = set()
        al_add = already.add
        for i in range(N):
            now = next_pos[i] - 1
            if now < 0:
                continue
            if now in already:
                continue
            if i == now:
            	continue
            if i in already:
                continue
            if next_pos[now] < 0:
            	continue

            if i == next_pos[now] - 1:
                if end:
                    end = False
                if A[i]:
                    next_pos[i] = A[i].pop(0)
                else:
                    next_pos[i] = -1
                if A[now]:
                    next_pos[now] = A[now].pop(0)
                else:
                    next_pos[now] = -1
                al_add(i)
                al_add(now)
        ans += 1
        if end:
            ans = -1
            break
        if next_pos == check:
            break
    print(ans)
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def main():
    N = int(input())
    # A = [list(map(int,input().split())) for _ in range(N)]
    A = []
    next_pos = []
    Apee = A.append
    neapp = next_pos.append
    for _ in range(N):
        npos, *l = list(map(int,input().split()))
        neapp(npos)
        Apee(l)
    # next_pos = [A[i].pop(0) for i in range(N)]
    ans = 0
    # check = [0 for _ in range(N)]
    check = [-1] * N
    while True:
        print(A)
        print(next_pos)
        end = True
        already = set()
        al_add = already.add
        for i in range(N):
            now = next_pos[i] - 1
            if now < 0:
                continue
            if now in already:
                continue
            if i == now:
            	continue
            if i in already:
                continue
            if next_pos[now] < 1:
            	continue

            if i == next_pos[now] - 1:
                if end:
                    end = False
                if A[i]:
                    next_pos[i] = A[i].pop(0)
                else:
                    next_pos[i] = -1
                if A[now]:
                    next_pos[now] = A[now].pop(0)
                else:
                    next_pos[now] = -1
                al_add(i)
                al_add(now)
        ans += 1
        if end:
            ans = -1
            break
        if next_pos == check:
            break
    print(ans)
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque

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

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

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

Submission status: Wrong Answer

Solution code:
N=int(input())
a=[]
b=[]
c=[0]*N
cnt=0
for i in range(N):
  a.append(list(map(int, input().split())))
  a[i].append(i+1)
#print(a)

for i in range(N):
  b.append(a[i][0])
#print('b='+str(b))
for j in range(N*(N-1)//2):
  matchfunc=lambda x: (x==b[b[x]-1]-1 and b[x]!=x+1)
  match=list(filter(matchfunc,range(N)))
  if match==[]:
    print(cnt)
    exit()
  #print(match)
  for i in range(len(match)):
    c[match[i]]+=1
    b[match[i]]=a[match[i]][c[match[i]]]
  #for i in range(N):
    #if b[i]==i+1:
      #cnt+=0
    #elif i==b[b[i]-1]-1:
      #c[i]+=1
  #print('c='+str(c))
  #for i in range(N):
    #b[i]=a[i][c[i]]
  #print('b='+str(b))
  cnt+=1
  #if c==[N-1]*N:
    #print(cnt)
    #exit()

print('-1')

Submission status: Wrong Answer

Solution code:
from collections import deque

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

class Cycle(Exception):
    pass

def dfs(x, y):
    Q = deque()
    Q.append((x, y, 1, set()))

    maxLen = 0
    seen = set()

    while Q:
        a, b, c, st = Q.pop()
        node = (min(a, b), max(a, b))

        if node in st:
            raise Cycle()

        if node in seen:
            continue
        seen.add(node)

        maxLen = max(c, maxLen)

        i = A[a].index(b)
        for j in range(i):
            Q.append((a, A[a][j], c + 1, st | set([node])))

        i = A[b].index(a)
        for j in range(i):
            Q.append((b, A[b][j], c + 1, st | set([node])))

    return maxLen

ans = -1
try:
    for i in range(N):
        ans = max(ans, dfs(i, A[i][-1]))
    print(ans)
except Cycle:
    print(-1)

Submission status: Wrong Answer

Solution code:
from collections import deque

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

class Cycle(Exception):
    pass

def dfs(x, y):
    Q = deque()
    Q.append((x, y, 1, []))

    maxLen = 0
    seen = set()

    while Q:
        a, b, c, st = Q.pop()
        node = (min(a, b), max(a, b))

        if node in st:
            raise Cycle()

        if node in seen:
            continue
        seen.add(node)

        maxLen = max(c, maxLen)

        i = A[a].index(b)
        for j in range(i):
            Q.append((a, A[a][j], c + 1, st + [node]))

        i = A[b].index(a)
        for j in range(i):
            Q.append((b, A[b][j], c + 1, st + [node]))

    return maxLen

ans = -1
try:
    for i in range(N):
        ans = max(ans, dfs(i, A[i][-1]))
    print(ans)
except Cycle:
    print(-1)

Submission status: Wrong Answer

Solution code:
from collections import deque

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

class Cycle(Exception):
    pass

def dfs(x, y):
    Q = deque()
    Q.append((x, y, 0))

    maxLen = 0

    stack = deque()

    while Q:
        a, b, c = Q.pop()

        if (min(a, b), max(a, b)) in stack:
            raise Cycle()

        stack.append((min(a, b), max(a, b)))
        maxLen = max(c, maxLen)

        hasChildren = False
        i = A[a].index(b)
        for j in range(i):
            hasChildren = True
            Q.append((a, A[a][j], c + 1))

        i = A[b].index(a)
        for j in range(i):
            hasChildren = True
            Q.append((b, A[b][j], c + 1))

        if not hasChildren:
            stack.pop()

    return maxLen

ans = -1
try:
    for i in range(N):
        ans = max(ans, dfs(i, A[i][-1]))
    print(ans)
except Cycle:
    print(-1)

Submission status: Wrong Answer

Solution code:
from collections import deque

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

class Cycle(Exception):
    pass

def dfs(x, y):
    Q = deque()
    Q.append((x, y, 1))

    maxLen = 0

    stack = deque()

    while Q:
        a, b, c = Q.pop()

        if (a, b) in stack:
            raise Cycle()

        stack.append((a, b))
        maxLen = max(c, maxLen)

        hasChildren = False
        i = A[a].index(b)
        for j in range(i):
            hasChildren = True
            Q.append((a, A[a][j], c + 1))

        i = A[b].index(a)
        for j in range(i):
            hasChildren = True
            Q.append((b, A[b][j], c + 1))

        if not hasChildren:
            stack.pop()

    return maxLen

ans = -1
try:
    for i in range(N):
        ans = max(ans, dfs(i, A[i][-1]))
    print(ans)
except Cycle:
    print(-1)

Submission status: Wrong Answer

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

day=0
jyunban=[i for i in range(n)]
match=[0 for i in range(n)]
while True:
  day+=1
  check=[True for i in range(n)]
  jyunban_n=[]
  if jyunban==[]:
    print(-1)
    sys.exit()
  
  for i in jyunban:
    if match[i]>=n-1:
      check[i]=False
    if check[i]==False:
      continue
    else:
      vs=a[i][match[i]]-1
      if check[i] and check[vs] and a[i][match[i]]-1==vs and a[vs][match[vs]]-1==i:
        match[i]+=1
        match[vs]+=1
        check[i]=False
        check[vs]=False
        jyunban_n.append(i)
        jyunban_n.append(vs)
      
    if i==len(jyunban)-1:
      if not False in check:
        print(-1)
        sys.exit()
  if min(match)==n-1 and min(match)==max(match):
    break
  jyunban=copy.copy(jyunban_n)
        
print(day)

Submission status: Wrong Answer

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

day=0
last_i=0
jyunban=[i for i in range(n)]
match=[0 for i in range(n)]
while True:
  day+=1
  check=[True for i in range(n)]
  jyunban_n=[]
  if jyunban==[]:
    print(-1)
    sys.exit()
  
  for i in jyunban:
    if match[i]>=n-1:
      check[i]=False
    if check[i]==False:
      continue
    else:
      vs=a[i][match[i]]-1
      if check[i] and check[vs] and a[i][match[i]]-1==vs and a[vs][match[vs]]-1==i:
        match[i]+=1
        match[vs]+=1
        check[i]=False
        check[vs]=False
        jyunban_n.append(i)
        jyunban_n.append(vs)
      
    if i==n-1:
      if not False in check:
        print(-1)
        sys.exit()
  if min(match)==n-1 and min(match)==max(match):
    break
  jyunban=copy.copy(jyunban_n)
        
print(day)

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque

n = int(input())

a = []
for i in range(n):
  v = deque(map(int, input().split(' ')))
  a.append(v)

day = 1
for ii in range(n*4):
  done = []
  nokori_games = 0
  
  for m in range(n):
    nokori_games += len(a[m])
    if m in done:
      pass
    elif len(a[m]):
      teki = a[m][0]
      jibun = m + 1
      if not len(a[teki - 1]):
        print(-1)
        sys.exit()
      elif a[teki - 1][0] == jibun and teki - 1 not in done:
        a[m].popleft()
        a[teki - 1].popleft()
        done.append(m)
        done.append(teki - 1)
        nokori_games -= 1
  if not nokori_games:
    print(day)
    sys.exit()
  else:
    if not len(done):
      print(-1)
      sys.exit()
  day += 1

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque

n = int(input())

a = []
for i in range(n):
  v = deque(map(int, input().split(' ')))
  a.append(v)

day = 1
for ii in range(n*2):
  done = []
  nokori_games = 0
  
  for m in range(n):
    nokori_games += len(a[m])
    if m in done:
      pass
    elif len(a[m]):
      teki = a[m][0]
      jibun = m + 1
      if not len(a[teki - 1]):
        print(-1)
        sys.exit()
      elif a[teki - 1][0] == jibun and teki - 1 not in done:
        a[m].popleft()
        a[teki - 1].popleft()
        done.append(m)
        done.append(teki - 1)
        nokori_games -= 1
  if not nokori_games:
    print(day)
    sys.exit()
  else:
    if not len(done):
      print(-1)
      sys.exit()
  day += 1

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque

n = int(input())

a = []
for i in range(n):
  v = deque(map(int, input().split(' ')))
  a.append(v)

day = 1
for ii in range(n):
  done = []
  nokori_games = 0
  
  for m in range(n):
    nokori_games += len(a[m])
    if m in done:
      pass
    elif len(a[m]):
      teki = a[m][0]
      jibun = m + 1
      if not len(a[teki - 1]):
        print(-1)
        sys.exit()
      elif a[teki - 1][0] == jibun and teki - 1 not in done:
        a[m].popleft()
        a[teki - 1].popleft()
        done.append(m)
        done.append(teki - 1)
        nokori_games -= 1
  if not nokori_games:
    print(day)
    sys.exit()
  else:
    if not len(done):
      print(-1)
      sys.exit()
  day += 1

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
def make(x,y):
    if x<y:
        return str(x)+"_"+str(y)
    else:
        return str(x)+"_"+str(y)


n = int(input())

s = set()
r = set()
edge = {}
redge = {}
for x in range(n):
    al = list(map(lambda x:int(x)-1, input().split()))
    s.add(make(x,al[0]))
    for i in range(len(al)-1):
        v = make(x,al[i])
        u = make(x,al[i+1])

        if v not in edge:
            edge[v] = []
        edge[v].append(u)
        if u not in redge:
            redge[u] = set()
        redge[u].add(v)
        r.add(u)

s = s - r

res = 0
while len(s)>0:
    res += 1
    s2 = set()
    for v in s:
        if v not in edge:
            continue
        for u in edge[v]:
            redge[u].remove(v)
            if len(redge[u])==0:
                s2.add(u)
    s = s2

for v in redge:
    if len(redge[v])>0:
        print(-1)
        exit()
print(res)

Submission status: Wrong Answer

Solution code:
import sys

n = int(input())

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

f = True
day = 1
while(f):
  done = []
  nokori = False
  for m in range(n):
    if m in done:
      if not nokori and len(a[m]):
        nokori = True
    elif len(a[m]):
      teki = a[m][0]
      jibun = m + 1
      if not len(a[teki - 1]):
        print(-1)
        sys.exit()
      elif a[teki - 1][0] == jibun and teki - 1 not in done:
        a[m].pop(0)
        a[teki - 1].pop(0)
        done.append(m)
        done.append(teki - 1)
        if not nokori and len(a[m]):
          nokori = True
  if not nokori:
    print(day)
    sys.exit()
  day += 1

Submission status: Wrong Answer

Solution code:
import sys

n = int(input())

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

f = True
day = 1
while(f):
  done = []
  nokori = False
  for m in range(n):
    if m in done:
      pass
    elif len(a[m]):
      teki = a[m][0]
      jibun = m + 1
      if not len(a[teki - 1]):
        print(-1)
        sys.exit()
      elif a[teki - 1][0] == jibun and teki - 1 not in done:
        a[m].pop(0)
        a[teki - 1].pop(0)
        done.append(m)
        done.append(teki - 1)
        if not nokori and len(a[m]):
          nokori = True
  if not nokori:
    for m in range(n):
      if len(a[m]):
        print(-1)
        sys.exit()
    print(day)
    sys.exit()
  day += 1

Submission status: Wrong Answer

Solution code:
import sys

n = int(input())

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

f = True
day = 1
while(f):
  done = []
  nokori = False
  for m in range(n):
    if m in done:
      pass
    elif len(a[m]):
      teki = a[m][0]
      jibun = m + 1
      if not len(a[teki - 1]):
        print(-1)
        sys.exit()
      elif a[teki - 1][0] == jibun and teki - 1 not in done:
        a[m].pop(0)
        a[teki - 1].pop(0)
        done.append(m)
        done.append(teki - 1)
      nokori = True
  if not nokori:
    for m in range(n):
      if len(a[m]):
        print(-1)
        sys.exit()
    print(day)
    sys.exit()
  day += 1

Submission status: Wrong Answer

Solution code:
import sys

n = int(input())

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

f = True
day = 1
while(f):
  done = []
  nokori = 0
  for m in range(n):
    if m in done:
      pass
    elif len(a[m]):
      teki = a[m][0]
      jibun = m + 1
      if not len(a[teki - 1]):
        print(-1)
        sys.exit()
      elif a[teki - 1][0] == jibun and teki - 1 not in done:
        a[m].pop(0)
        a[teki - 1].pop(0)
        done.append(m)
        done.append(teki - 1)
        nokori += len(a[m])
  if not nokori:
    for m in range(n):
      if len(a[m]):
        print(-1)
        sys.exit()
    print(day)
    sys.exit()
  day += 1

Submission status: Wrong Answer

Solution code:
import sys

n = int(input())

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

f = True
day = 1
while(f):
  done = []
  for m in range(n):
    if m in done:
      continue
    if len(a[m]):
      teki = a[m][0]
      jibun = m + 1
      if not len(a[teki - 1]):
        print(-1)
        sys.exit()
      elif a[teki - 1][0] == jibun and teki - 1 not in done:
        a[m].pop(0)
        a[teki - 1].pop(0)
        done.append(m)
        done.append(teki - 1)

  if not len(done):
    for m in range(n):
      if len(a[m]):
        print(-1)
        sys.exit()
    print(day)
    sys.exit()
  day += 1

Submission status: Wrong Answer

Solution code:
import sys

n = int(input())

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

f = True
day = 1
while(f):
  do = False  
  done = []
  for m in range(n):
    if len(a[m]):
      teki = a[m][0]
      jibun = m + 1
      if not len(a[teki - 1]):
        print(-1)
        sys.exit()
      elif a[teki - 1][0] == jibun and teki - 1 not in done:
        a[m].pop(0)
        a[teki - 1].pop(0)
        do = True
        done.append(m)
        done.append(teki - 1)

  if not do:
    for m in range(n):
      if len(a[m]):
        print(-1)
        sys.exit()
    print(day)
    sys.exit()
  day += 1

Submission status: Wrong Answer

Solution code:
import sys

n = int(input())

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

f = True
day = 1
while(f):
  do = False
  for m in range(n):
    done = []
    if len(a[m]):
      teki = a[m][0]
      jibun = m + 1
      if not len(a[teki - 1]):
        print(-1)
        sys.exit()
      elif a[teki - 1][0] == jibun and teki - 1 not in done:
        a[m].pop(0)
        a[teki - 1].pop(0)
        do = True
        done.append(m)
        done.append(teki - 1)

  if not do:
    for m in range(n):
      if len(a[m]):
        print(-1)
        sys.exit()
    print(day)
    sys.exit()
  day += 1

Submission status: Wrong Answer

Solution code:
import sys

n = int(input())

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

f = True
day = 1
while(f):
  do = False
  for m in range(n):
    done = []
    if len(a[m]):
      teki = a[m][0]
      jibun = m + 1
      if not len(a[teki - 1]):
        print(-1)
        sys.exit()
      elif a[teki - 1][0] == jibun and teki - 1 not in done:
        a[m].pop(0)
        a[teki - 1].pop(0)
        do = True
        done.append(teki - 1)
  if not do:
    for m in range(n):
      if len(a[m]):
        print(-1)
        sys.exit()
    print(day)
    sys.exit()
  day += 1

Submission status: Wrong Answer

Solution code:
import sys

n = int(input())

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

f = True
day = 1
while(f):
  do = False
  for m in range(n):
    if len(a[m]):
      teki = a[m][0]
      jibun = m + 1
      if not teki:
        a[m].pop(0)
        do = True
      elif not len(a[teki - 1]):
        print(-1)
        sys.exit()
      elif a[teki - 1][0] == jibun:
        a[m].pop(0)
        a[teki - 1][0] = 0
        do = True
  if not do:
    for m in range(n):
      if len(a[m]):
        print(-1)
        sys.exit()
    print(day)
    sys.exit()
  day += 1

Submission status: Wrong Answer

Solution code:
import sys

n = int(input())

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

f = True
day = 1
while(f):
  do = False
  for m in range(n):
    if len(a[m]):
      teki = a[m][0]
      jibun = m + 1
      if not teki:
        a[m].pop(0)
      elif not len(a[teki - 1]):
        print(-1)
        sys.exit()
      elif a[teki - 1][0] == jibun:
        a[m].pop(0)
        a[teki - 1][0] = 0
        do = True
  if not do:
    for m in range(n):
      if len(a[m]):
        print(-1)
        sys.exit()
    print(day)
    sys.exit()
  day += 1

Submission status: Wrong Answer

Solution code:
import sys

n = int(input())

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

f = True
day = 1
while(f):
  do = False
  for m in range(n):
    if len(a[m]):
      teki = a[m][0]
      jibun = m + 1
      if not teki:
        a[m].pop(0)
      elif a[teki - 1][0] == jibun:
        a[m].pop(0)
        a[teki - 1][0] = 0
        do = True
  if not do:
    for m in range(n):
      if len(a[m]):
        print(-1)
        sys.exit()
    print(day)
    sys.exit()
  day += 1

Submission status: Wrong Answer

Solution code:
n=int(input())
print(int(((n-1)*n)//2))

Submission status: Wrong Answer

Solution code:
n=int(input())
print(int(((n-1)*n)/2))

Submission status: Wrong Answer

Solution code:
import sys
def solve(matrix):
    N = len(matrix)
    #print  matrix
    day = 0
    worst = N * (N-1) / 2
    remaining = worst
    while remaining:
        prev = remaining
        day+=1
        #print "day %d" % day
        #print "matrix: %r" % matrix
        #print "remaining:%d" % remaining
        match = {}
        for i, order in enumerate(matrix):
            me = i+1
            if match.get(me) and order and order[0] in match[me]:
                x = matrix[order[0]-1].pop(0)
                y = order.pop(0)
                remaining -= 1
                #print "%d and %d" % (x, y)
                #print "matrix: %r" % matrix
            elif order:
                if match.get(order[0]):
                    match[order[0]].append(me)
                else:
                    match[order[0]] = [me]
                #print 'match: %r' % match
        if remaining  == prev:
            print -1
            return
    print day

Submission status: Wrong Answer

Solution code:
import sys
def solve(matrix):
    N = len(matrix)
    print  matrix
    day = 0
    worst = N * (N-1) / 2
    remaining = worst
    while remaining:
        prev = remaining
        day+=1
        print "day %d" % day
        print "matrix: %r" % matrix
        print "remaining:%d" % remaining
        match = {}
        for i, order in enumerate(matrix):
            me = i+1
            if match.get(me) and order and order[0] in match[me]:
                x = matrix[order[0]-1].pop(0)
                y = order.pop(0)
                remaining -= 1
                print "%d and %d" % (x, y)
                print "matrix: %r" % matrix
            elif order:
                if match.get(order[0]):
                    match[order[0]].append(me)
                else:
                    match[order[0]] = [me]
                print 'match: %r' % match
        if remaining  == prev:
            print -1
            sys.exit(0)
    print day