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: Time Limit Exceeded

Solution code:
import sys

input = sys.stdin.readline


class AtCoder:
    def main(self):
        N = int(input())
        A = [list(map(int, input().split())) for _ in range(N)]

        already_p = set()
        days = 0
        done = set()
        while True:
            for i, a in enumerate(A):
                if len(a) == 0:
                    done.add(i + 1)
                    continue
                if i + 1 not in already_p and a[0] not in already_p and A[a[0] - 1][0] == i + 1:
                    already_p.add(i + 1)
                    already_p.add(a[0])
                    A[a[0] - 1] = A[a[0] - 1][1:]
                    A[i] = a[1:]

            if len(done) == N:
                break

            if len(already_p) == 0:
                print(-1)
                exit()

            already_p = set()
            days += 1
            if days == (N * (N - 1)) // 2:
                print(days)
                exit()

        print(days)


if __name__ == '__main__':
    AtCoder().main()

Submission status: Time Limit Exceeded

Solution code:
n = int(input())

a = [[0 for j in range(n-1)] for i in range(n)]
for i in range(n):
  tmp  = list(map(int, input().split()))
  for j in range(n-1):
    a[i][j] = tmp[j]

cnt = 0
day = 0

do   = []
done = []
for i in range(n):
  do.append(i)

while cnt < n * (n-1) / 2:
  day += 1
  flg = 0
  
  for i in do:
    if len(a[i]) > 0:
      if ((a[i][0] - 1) not in done) and (a[a[i][0]-1][0]-1 == i):
#        print('DEBUG HIT '+str(i)+':'+str(a[i][0])+':'+str(a[a[i][0]-1][0]))
        done.append(i)
        done.append(a[i][0]-1)
        flg = 1
        cnt += 1

  if flg == 0:
    day = -1
    break
  else:
    do.clear
    while(len(done) != 0):
      tmp = done.pop(0)
      do.append(tmp)
      del a[tmp][0]
    
print(day)

Submission status: Time Limit Exceeded

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

k=0
while(True):
    k+=1
    for i in range(N):
        if(A[i]==0):
            if(A[H[i][0]-1]==0):
                a=H[i][0]-1
                b=H[H[i][0]-1][0]
                if(H[H[i][0]-1][0]==i+1):
                    A[i]=1
                    A[H[i][0]-1]=1
                    if(len(H[H[i][0]-1])==1):
                        A2[H[i][0]-1]=1
                    if(len(H[i])==1):
                        A2[i]=1

                    H[H[i][0]-1].pop(0)
                    H[i].pop(0)
                    
                    flag2=0

    for j in range(N):
        A[j] = A2[j]
        if(A2[j]==0):
            flag=0
    
    if(flag==1):
        break
    if(flag2==1):
        break
    
    flag=1
    flag2=1

if(flag2==1):
    print("-1")
    exit()

print(k)

Submission status: Time Limit Exceeded

Solution code:
def main():
    from collections import deque
    import sys
    input = sys.stdin.readline

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

    ans = 0
    count = 0
    MAX_COUNT = n * (n-1)
    iteration_times = 0
    flag = 0

    while iteration_times <= MAX_COUNT:
        # print(L)
        # print(count)
        iteration_times += 1

        if count == MAX_COUNT:
            flag = 1
            break
        
        numset = set()
        add = numset.add
        for i in range(n):
            if len(L[i]) != 0:
                a = L[i][0]
                b = L[a][0]
                if i == b:
                    add(i)
                    add(b)
        for elm in numset:
            L[elm].popleft()
            count += 1

        ans += 1

    if flag == 1:
        print(ans)
    else:
        print(-1)


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
# ABC139e
import sys
import collections
input = sys.stdin.readline
sys.setrecursionlimit(10**6)

n = int(input())
a = [collections.deque(map(int, input().split())) for _ in range(n)]
pos = [0] * n

day = 0
# print(a)
while (True):
    l = set()
    for i in range(n):
        if (pos[i] < n-1):
            aite = a[i][pos[i]]-1
            if (a[aite][pos[aite]]-1 == i):
                l.add(i)
    if (len(l) == 0):
        break
    for i in l:
        pos[i] += 1
    # print(l)
    l.clear()
    day += 1
    # print(a)
if(max(pos) == n-1):
    print(day)
else:
    print(-1)

Submission status: Time Limit Exceeded

Solution code:


from collections import deque, defaultdict
import copy
import bisect
#sys.setrecursionlimit(10 ** 9)
import math
import heapq

#N = list(map(int, input().split()))

import sys
def input():
	return sys.stdin.readline().strip()


N = int(input())
A = []
day = [0]*N
for i in range(N):
	A.append(deque(map(int, input().split())))
num = 0
i = 0
judge = 0
rest = [N - 1]*N
for _ in range(10000000000):
	judge += 1
	i = i % N
	if rest[i] > 0:
		j = A[i][0] - 1
		if rest[j] > 0:
			if A[j][0] == i + 1:
				A[i].popleft()
				A[j].popleft()
				rest[i] -= 1
				rest[j] -= 1
				shiai = max(day[i], day[j]) + 1
				day[i] = shiai
				day[j] = shiai
				num += 1
				judge = 0

	i += 1

	if judge >= N:
		break


if num == N*(N - 1)//2:
	print(max(day))
else:
	print(-1)

Submission status: Time Limit Exceeded

Solution code:
import sys
from collections import deque

sys.setrecursionlimit(10000000)

n = int(sys.stdin.readline().rstrip())
a = [deque(map(int, sys.stdin.readline().split())) for _ in range(n)]

def deepPop(x):
    global a
    global days
    target = a[x][0] -1
    if a[target][0] -1 == x:
        a[target].popleft()
        a[x].popleft()
        maxd = max(days[x] +1, days[target] +1)
        days[x] = maxd
        days[target] = maxd
        if len(a[x]) > 0:
            deepPop(x)
        if len(a[target]) > 0:
            deepPop(target)


day = 0
days = [0] * n
setn = set(range(n))
while True:
    popped = 0
    zeros = 0
    tmpns = list(setn)
    for i in tmpns:
        if len(a[i]) == 0:
            setn.remove(i)
            continue
        num = a[i][0] - 1
        if num < i:
            continue
        if a[num][0] - 1 == i:
            deepPop(i)
            popped += 1
    if popped == 0:
        if len(setn) == 0:
            break
        else:
            day = -1
            break

if day == -1:
    print(day)
else:
    print(max(days))