littlep001 @ 2024-09-22 18:25:33
#include <bits/stdc++.h>
using namespace std;
class fastIO{private:char ibuf[50007],*p1=ibuf,*p2=ibuf,obuf[50007],*p3=obuf,sta[50];bool file_end=false;char get(){return p1==p2&&(p2=(p1=ibuf)+fread(ibuf,1,50007,stdin),p1==p2)?(file_end=true),char(EOF):*p1++;}void put(const char x){p3-obuf<50007?*p3++=x:(fwrite(obuf,p3-obuf,1,stdout),p3=obuf,*p3++=x);}public:explicit operator bool(){return!file_end;}size_t flush(){size_t f=fwrite(obuf,p3-obuf,1,stdout);p3=obuf;*p3=0;return f;}fastIO&operator>>(char&t){for(t=get();!isgraph(t);t=get()){;}return*this;}template<typename any>typename std::enable_if<std::is_same<any,char>::value,any>::type tpval(){char t;for(t=get();!isgraph(t);t=get()){;}return t;}fastIO&operator>>(char*t){char c;for(c=get();!isgraph(c);c=get()){;}for(;isgraph(c);c=get())*t=c,t++;*t=0;return*this;}fastIO&operator>>(std::string&t){t.clear();char c;for(c=get();!isgraph(c);c=get()){;}for(;isgraph(c);c=get())t+=c;return*this;}template<typename any>typename std::enable_if<std::is_same<any,std::string>::value,any>::type tpval(){std::string t;char c;for(c=get();!isgraph(c);c=get()){;}for(;isgraph(c);c=get())t+=c;return t;}template<typename any>typename std::enable_if<(std::is_signed<any>::value&&std::is_integral<any>::value&&!std::is_same<any,char>::value)||std::is_same<any,__int128_t>::value,fastIO>::type&operator>>(any&t){t=0;bool y=0;char c=get();for(;!isdigit(c);c=get())if(c==45)y=true;for(;isdigit(c);c=get())t=t*10+c-48;if(y==1)t=-t;return*this;}template<typename any>typename std::enable_if<(std::is_signed<any>::value&&std::is_integral<any>::value&&!std::is_same<any,char>::value)||std::is_same<any,__int128_t>::value,any>::type tpval(){any t=0;bool y=0;char c=get();for(;!isdigit(c);c=get())if(c==45)y=true;for(;isdigit(c);c=get())t=t*10+c-48;if(y==1)t=-t;return t;}template<typename any>typename std::enable_if<(std::is_unsigned<any>::value&&std::is_integral<any>::value&&!std::is_same<any,char>::value)||std::is_same<any,__uint128_t>::value,fastIO>::type&operator>>(any&t){t=0;char c=get();for(;!isdigit(c);c=get()){;}for(;isdigit(c);c=get())t=t*10+c-48;return*this;}template<typename any>typename std::enable_if<(std::is_unsigned<any>::value&&std::is_integral<any>::value&&!std::is_same<any,char>::value)||std::is_same<any,__uint128_t>::value,any>::type tpval(){any t=0;char c=get();for(;!isdigit(c);c=get()){;}for(;isdigit(c);c=get())t=t*10+c-48;return t;}template<typename any1,typename any2>fastIO&operator>>(std::pair<any1,any2>&t){return*this>>t.first>>t.second;}template<typename any1,typename any2>std::pair<any1,any2>tpval(){return std::pair<any1,any2>(tpval<any1>(),tpval<any2>());}template<typename any>fastIO&read(any&t){return*this>>t;}fastIO&read(char*t){char c;for(c=get();!isgraph(c);c=get()){;}for(;isgraph(c);c=get())*t=c,t++;*t=0;return*this;}template<typename any,typename...args>fastIO&read(any&t1,args&...t2){return(*this>>t1).read(t2...);}fastIO&operator<<(const char t){put(t);return*this;}fastIO&operator<<(const char*t){for(;*t;t++)put(*t);return*this;}fastIO&operator<<(const std::string&t){for(const char it:t)put(it);return*this;}template<typename any>typename std::enable_if<(std::is_signed<any>::value&&std::is_integral<any>::value&&!std::is_same<any,char>::value)||std::is_same<any,__int128_t>::value,fastIO>::type&operator<<(any t){if(!t){put(48);return*this;}int len=0;if(t<0)t=-t,put(45);while(t)sta[len++]=char(t%10+48),t/=10;while(len--)put(sta[len]);return*this;}template<typename any>typename std::enable_if<(std::is_unsigned<any>::value&&std::is_integral<any>::value&&!std::is_same<any,char>::value)||std::is_same<any,__uint128_t>::value,fastIO>::type&operator<<(any t){if(!t){put(48);return*this;}int len=0;while(t)sta[len++]=char(t%10+48),t/=10;while(len--)put(sta[len]);return*this;}template<typename any1,typename any2>fastIO&operator<<(const std::pair<any1,any2>&t){return*this<<t.first<<' '<<t.second;}template<typename any>fastIO&write(const any&t){return*this<<t;}template<typename any,typename...args>fastIO&write(const any&t1,const args&...t2){return(*this<<t1).write(t2...);}~fastIO(){fwrite(obuf,p3-obuf,1,stdout);}}fio;
const int N=1e3+5;
int g[N][N],v[N],inq[N],vis[N],t[N],tp;
int main(){
int n,m;
fio>>n>>m;
for(int i=1;i<=m;i++){
int len,t1,t2;
fio>>len>>t1;
vis[t1]=1;
tp=0;
t[++tp]=t1;
for(int j=2;j<=len;j++){
fio>>t2;
vis[t2]=1;
t[++tp]=t2;
}
for(int j=1;j<=tp;j++){
for(int k=t1+1;k<t2;k++){
if(!vis[k]){
g[t[j]][k]=1;
}
}
}
for(int j=1;j<=tp;j++)
{
vis[t[j]]=0;
}
}
queue<int> q;
for(int i=1;i<=n;i++){
q.push(i);
inq[i]=1;
v[i]=1;
}
while(!q.empty()){
int x=q.front();
inq[x]=0;
q.pop();
for(int i=1;i<=n;i++){
if(g[i][x]&&v[i]<=v[x]){
v[i]=v[x]+1;
if(!inq[i]){
q.push(i);
inq[i]=1;
}
}
}
}
int ans=0;
for(int i=1;i<=n;i++){
ans=max(ans,v[i]);
}
fio<<ans<<"\n";
return 0;
}
用的邻接矩阵SPFA,结果过了
by littlep001 @ 2024-09-22 18:27:54
理论SPFA可以被卡到
by suzhikz @ 2024-09-22 19:01:20
对于 100\%100% 的数据,1 ≤ n, m ≤ 10001≤n,m≤1000。 @littlep001
by littlep001 @ 2024-09-22 19:07:46
@suzhikz
by littlep001 @ 2024-09-22 19:08:25
而且我不卡常就最慢点1.18s
by littlep001 @ 2024-09-22 20:01:46
@suzhikz 这里我的m不是题意中的m,我的m是图中我可能建的边的数量,抱歉表达有误
by Reject @ 2024-10-18 11:24:00
有没有一种可能,边权为 1 的图, SPFA 其实和 BFS 几乎是一样的啊。。。