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.

Solution (Python):
import sys
stdin = sys.stdin

sys.setrecursionlimit(10 ** 7)

def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x) - 1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

from collections import deque


n = ni()
graph = [[] for _ in range(n*n)]


ins = [0]*(n*n)
for i in range(n):
    jlist = list(li_())
    for j1, j2 in zip(jlist[:-1], jlist[1:]):
        node1 = max(i, j1)*n + min(i, j1)
        node2 = max(i, j2)*n + min(i, j2)
        graph[node1].append(node2)
        ins[node2] += 1


visited = [False]*(n*n)
for i in range(n):
    visited[i*n + i] = True


que = deque()
for i, insi in enumerate(ins):
    if insi == 0:
        que.append((i, 1))


dist = [0]*(n*n)
while que:
    cur, d = que.popleft()

    visited[cur] = True
    dist[cur] = d
    for nex in graph[cur]:
        if not visited[nex]:
            ins[nex] -= 1
            if ins[nex] == 0:
                que.append((nex, d+1))


ok = True
for vi in visited:
    if not vi:
        ok = False


ans = 0
for di in dist:
    ans = max(ans, di)

print(-1 if not ok else ans)

Solution (Python):
N = int(input())
A = [[]] + [list(map(int, input().split())) for _ in range(N)]

c = [0 for _ in range(N + 1)]
p = []
for i in range(1, N + 1):
    j = A[i][c[i]]
    if i == A[j][c[j]] and i < j:
        p.append((i, j))
ans = 0
while p:
    
    q = []
    for i, j in p:
        c[i] += 1
        c[j] += 1
        if c[i] < N - 1:
            k = A[i][c[i]]
            if A[k][c[k]] == i:
                q.append((i, k))
        if c[j] < N - 1:
            k = A[j][c[j]]
            if A[k][c[k]] == j:
                q.append((j, k))
    ans += 1
    p = q

ans = (
    ans if all(c[i] == N - 1 for i in range(1, N + 1)) else
    -1
)
print(ans)

Solution (Python):
rr = raw_input
rri = lambda: int(raw_input())
rrm = lambda: map(int, raw_input().split())

from collections import deque
def solve(N, A):
    ans = 0
    todo = N * (N-1)
    to_inspect = range(N)
    while True:
        ans += 1
        found = False
        to_inspect2 = set()
        for i in to_inspect:
            row = A[i]
            if row:
                j = row[0]
                if (i not in to_inspect2 and j not in to_inspect2 and
                        A[j] and A[j][0] == i):
                    row.popleft()
                    A[j].popleft()
                    found = True
                    todo -= 2
                    to_inspect2.add(i)
                    to_inspect2.add(j)
        if not todo: return ans
        if not found: return -1
        to_inspect = to_inspect2


N = rri()
A = []
for _ in xrange(N):
    row = rrm()
    A.append(deque(x-1 for x in row))
print solve(N, A)

Solution (Python):
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,copy,functools
import time

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(): return [list(map(int, l.split())) for l in sys.stdin.readlines()]
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 I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)
def pe(s): return print(str(s), file=sys.stderr)
def JA(a, sep): return sep.join(map(str, a))
def JAA(a, s, t): return s.join(t.join(map(str, b)) for b in a)

def main():
    n = I()
    a = [LI_() for _ in range(n)]
    r = 0
    c = collections.defaultdict(int)
    q = list(range(n))
    t = [0] * n
    while q:
        r += 1
        nq = []
        for i in q:
            j = a[i][t[i]]
            k = i
            if k < j:
                j,k = k,j
            c[(j,k)] += 1
            if c[(j,k)] == 2:
                t[j] += 1
                t[k] += 1
                if t[j] < n-1:
                    nq.append(j)
                if t[k] < n-1:
                    nq.append(k)
        q = nq

    for tt in t:
        if tt != n-1:
            return -1

    return r


print(main())

Solution (Python):
import sys

stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  

n = ni()
a = []
for i in range(n):
    a.append(na())

def code(l, r, of):
    if l > r:
        l, r = r, l
    return of[l] + r-l-1

of = [0] * (n+1)
for i in range(n):
    of[i+1] = of[i] + n-i-1
g = [[] for _ in range(n*(n-1)//2)]
for i in range(n):
    for j in range(n-2):
        fcode = code(i, a[i][j]-1, of)
        tcode = code(i, a[i][j+1]-1, of)
        g[fcode].append(tcode)

def sort_topologically(g):
    n = len(g)
    ec = [0] * n
    for i in range(n):
        for e in g[i]:
            ec[e] += 1
    ret = [0] * n
    q = 0

    for i in range(n):
        if ec[i] == 0:
            ret[q] = i
            q += 1

    p = 0
    while p < q:
        for to in g[ret[p]]:
            ec[to] -= 1
            if ec[to] == 0:
                ret[q] = to
                q += 1
        p += 1

    for i in range(n):
        if ec[i] > 0:
            return None
    return ret

od = sort_topologically(g)
if not od:
    print(-1)
else:
    dp = [1] * (n*(n-1)//2)
    for i in range(n*(n-1)//2):
        for e in g[od[i]]:
            dp[e] = max(dp[e], dp[od[i]] + 1)
    print(max(dp))