sczh @ 2019-11-23 08:19:46
#include<bits/stdc++.h>
using namespace std;
queue<int>q;
int n,m,_top,lin_vis[10005],top_vis[10005]
,to[10005],next[10005],fir[10005],in[10005];
void add_edge(int x,int y){
_top++;in[y]++;
to[_top]=y;next[_top]=fir[x];fir[x]=_top;
}
void top_delete(int x){
top_vis[x]=1;int t=fir[x];
while(t){in[to[t]]--;t=next[t];}
}
int main(){
int death=0;cin>>n>>m;
for(int i=1,s,begin,end;i<=m;i++){
memset(lin_vis,0,sizeof(lin_vis));
cin>>s;
for(int j=1,f;j<=s;j++){
cin>>f;lin_vis[f]=1;
if(!(j-1))begin=f;
if(!(j-s))end=f;
}
for(int j=begin;j<=end-1;j++)
if(lin_vis[j])
for(int k=begin;k<=end-1;k++)
if(!lin_vis[k])
add_edge(k,j);
}
for(;;){
for(int i=1;i<=n;i++)
if(!top_vis[i]&&!in[i])
q.push(i);
if(q.empty())break;
while(!q.empty()){top_delete(q.front());q.pop();}
death++;
}
cout<<death;
return 0;
}