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 in C++14 (GCC 5.4.1) (judge verdict: Accepted)

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
vector<int>arr[1000000+100]; 
bool has[1000000+100]={false};
int indegree[1000000+100];
int diss[1000000+100];
int zh(int a,int b)
{
	return a*1000+b;
}
bool flag=false;
int my_max(int a,int b)
{
	return a>b?a:b;
}
void  dfs(int star,int now,int fa)
{
	has[now]=true;
	if(now==star)
	{
		flag=true;
		return ;
	}
//	printf("%d--\n",now);
	for(int i=0;i<arr[now].size();i++)
	{
		int v=arr[now][i];
	//	printf("%d\n",v); 
		if(v!=fa && !has[v])
			dfs(star,v,now);
		if(flag)
			return ;
	}
}
int dfs2(int now)
{
	int an=1;
	int u=0;
	has[now]=true;
	for(int i=0;i<arr[now].size();i++)
	{
		int v=arr[now][i];
		if(!has[v])
			u=my_max(u,dfs2(v));
		else
			u=my_max(u,diss[v]);
	}
	return diss[now]=u+1;
}
int low[1000000+100];
int dfn[1000000+100];
int ins[1000000+100];
int tot=0;
stack<int>s;
void tarjan(int x)
{
    low[x]=dfn[x]=++tot;//
    s.push(x);ins[x]=1;
    for(int i=0;i<arr[x].size();i++){
        int p=arr[x][i];
        if(!dfn[p]){//判断点是否被访问过
            tarjan(p);
            low[x]=min(low[x],low[p]);
        }
        else if(ins[p]) low[x]=min(low[x],dfn[p]);//判断点是否在栈中
    }
 
    if(low[x]==dfn[x]){//dfs到叶子节点，开始判断，如果==就到达了某个强联通分量的根节点,dfs回溯到了这个点
        int cnt=0;
        while(1){//得出相关的强联通分量
            int now=s.top();
            s.pop();
            ins[x]=0;//出栈visit置0
            cnt++;
            if(now==x) break;//通过栈找到那个点
        }
        if(cnt>1) flag=true;
    	
    }
}
int main()
{
	int n;
	scanf("%d",&n);
	int p=n;
	for(int i=0;i<n;i++)
	{
		int a;
		scanf("%d",&a);
		a--;
		int b=i;
		if(a>b)
		{
			int c=a;
			a=b;
			b=c;
		}
		int last=zh(a,b);
		for(int k=0;k<n-2;k++)
		{
			scanf("%d",&a);
			a--;
			b=i;
			if(a>b)
			{
				int c=a;
				a=b;
				b=c;
			}
			int node=zh(a,b);
			arr[node].push_back(last);
			indegree[last]++;
			last=node;
		}
	}
	memset(has,false,sizeof(has));
	for(int i=0;i<=1000000+5;i++)
	{
		if(!dfn[i])
		{
			tarjan(i);
			if(flag)
				break;
		}
	}
//	printf("%d",flag);
	if(flag==true)
		printf("%d\n",-1);
	else
	{
	//	printf("asd");
		memset(has,false,sizeof(has));
		for(int i=0;i<=1000000+5;i++)
			diss[i]=0;
		int ans=1;
		for(int i=0;i<n;i++)
			for(int k=0;k<i;k++)
			{
				if(indegree[zh(k,i)]==0)
				{
				//	has[zh(k,i)]=true;
					diss[zh(k,i)]=1;
					ans=my_max(ans,dfs2(zh(k,i)));
				//	printf("%d\n",zh(k,i));
				}
			}
	//	printf("asd");
		
		printf("%d\n",ans);
	}
	return 0;
 }