ljkgs6789 @ 2024-07-17 20:08:14
#include<bits/stdc++.h>
using namespace std;
template<typename T> inline void read(T& x){
x=0;int f=1;
char ch=getchar();
while(!isdigit(ch)){if(ch=='-') f=~f+1;ch=getchar();}
while(isdigit(ch)){x=(x<<3)+(x<<1)+(ch^48);ch=getchar();}
x*=f;
}
template<typename T,typename... Args>inline void read(T& x,Args&... args){
read(x);
read(args...);
}
template<typename T>inline void write(T x){
static int buf[40],top=0;
if(x<0){putchar('-');x=~x+1;}
while(x)buf[++top]=x%10,x/=10;
if(top==0)buf[++top]=0;
while (top) putchar(buf[top--]^48);
putchar(' ');
}
template<typename T,typename... Args>inline void write(T x,Args... args){
write(x);
write(args...);
}
int n,m,s,a[1010],in[1010],ans;
int used[1010],cnt,tail[1010];
struct node{
int u,v,next;
}ed[1010];
struct node1{
int dep,id;
};
void add(int u,int v){
ed[++cnt].u=u;
ed[cnt].v=v;
ed[cnt].next=tail[u];
tail[u]=cnt;
}
queue<node1>q;
int main(){
read(n);read(m);
for(int i=1;i<=n;i++){
in[i]=-1;
}
for(int i=1;i<=m;i++){
read(s);
for(int i=1;i<=n;i++){
used[i]=0;
}
for(int j=1;j<=s;j++){
read(a[j]);
used[a[j]]++;
}
for(int j=a[1];j<=a[s];j++){
if(in[j]==-1)in[j]=0;
if(used[j]==0){
for(int k=1;k<=s;k++){
add(a[k],j);
in[j]++;
}
}
}
}
for(int i=1;i<=n;i++){
if(in[i]==0)q.push({1,i});
}
while(!q.empty()){
node1 now=q.front();
q.pop();
if(in[now.id]==-1)continue;
in[now.id]=-1;
ans=max(ans,now.dep);
for(int i=tail[now.id];i;i=ed[i].next){
int to=ed[i].v;
in[to]--;
if(in[to]==0){
q.push({now.dep+1,to});
}
}
}
write(ans);
return 0;
}
by NullPointerExpection @ 2024-07-17 20:09:37
@ljkgs6789
by ljkgs6789 @ 2024-07-17 20:20:42
@NullPointerExpection 我看一些ti jie也是O(n^3)
by xmy201315 @ 2024-07-17 20:32:14
@ljkgs6789,我认为最简单的方法是有向无环图,然后是拓扑排序+dp,如果你不会,就可以用记忆化搜索
by Lijunzhuo @ 2024-07-22 16:53:58
兄弟,你越界了,你不错谁错啊 @ljkgs6789