info___tion @ 2018-09-28 17:02:06
各种优化都加了,还是
#include<stdio.h>
#include<string.h>
#define max(x,y) (x)>(y)?x:y
const int maxn=1002;
bool Map[maxn][maxn];
int a[maxn];
bool table[maxn];
struct edge
{
int to,val;
};
int head[maxn],Next[maxn*maxn];
edge G[maxn*maxn];
int sz;
void Insert(int from,edge e)
{
G[++sz]=e;
Next[sz]=head[from];
head[from]=sz;
return;
}
int dis[maxn];
int deg[maxn];
struct Qnode
{
int pos,val;
}que[10002];
int n;
void bfs()
{
for(int i=1;i<=n;i++)
dis[i]=1;
int h=0,t=0;
for(int i=1;i<=n;i++)
if(!deg[i]) que[t++]=(Qnode){i,1};
while( h^t )
{
Qnode H=que[h++];
h%=10001;
for(int i=head[H.pos];i;i=Next[i])
{
edge e=G[i];
deg[e.to]--;
if( !deg[e.to] )
{
dis[e.to]=H.val+1;
que[t++]=(Qnode){e.to,H.val+1};
t%=10001;
}
}
}
return;
}
int Read()
{
char c=getchar();
while( c<'0' or '9'<c ) c=getchar();
int res=0;
while( '0'<=c and c<='9' ) res=(res<<3)+(res<<1)+c-'0',c=getchar();
return res;
}
char buf[10];
int buf_sz;
void Write(int x)
{
buf_sz=0;
while(x>0) buf[buf_sz++]=x%10+'0',x/=10;
while( buf_sz-- ) putchar(buf[buf_sz]);
return;
}
int main(int argc, char const *argv[])
{
int m;
n=Read(),m=Read();
for(int i=1;i<=m;i++)
{
memset( table,false,sizeof(table) );
int len;
len=Read();
int l,r;
for(int j=1;j<=len;j++)
{
a[j]=Read();
if(j==1) l=a[j];
else if(j==len) r=a[j];
table[a[j]]=true;
}
for(int j=1;j<=len;j++)
for(int k=l+1;k<r;k++)
{
if( a[j]==k or table[k] or Map[a[j]][k] ) continue;
Map[a[j]][k]=true;
Insert( a[j],(edge){k,1} );
deg[k]++;
}
}
bfs();
int ans=1;
for(int i=1;i<=n;i++)
ans=max( ans,dis[i] );
Write(ans);
return 0;
}
by hanjicheng @ 2018-09-28 17:34:02
很好奇我dfs+0优化就过了
by fmj_123 @ 2018-09-29 13:03:13
@info___tion 很好奇我暴力建边 0优化就过了 跑的飞快
by fmj_123 @ 2018-09-29 13:05:37
还有,我用的是cin
by Ureka_Gestalt @ 2018-10-16 13:58:57
@fmj_123 %%%输入输出流神仙
by PRAGMATISM @ 2020-08-02 15:43:49
我铲