1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include<bits/stdc++.h>
using namespace std;
int main(){
	int n,i,j,m,result,w,tmp;
	scanf("%d",&n);
	vector <int> G[n+1];
	int arr[n+1];
	for(i=1;i<=n;i++){
		scanf("%d",&m);
		G[i].resize(m);
		for(j=0;j<m;j++)
			scanf("%d",&G[i][j]);
		arr[i]=0;
	}
	tmp=0;
	result=0;
	do{
		w=1;
		while(G[w].size()){
			if(arr[w]==0)
				tmp++;
			if((arr[w]+1)%G[w].size()==0)
				tmp--;
			arr[w]=(arr[w]+1)%G[w].size();
			w=G[w][(arr[w]-1+G[w].size())%G[w].size()];
		}
		result++;
	}while(tmp);
	printf("%d\n",result);
	return 0;
}