玄关,求调 Runtime error on test 7
查看原帖
玄关,求调 Runtime error on test 7
575363
revolutionary_oier楼主2025/1/16 15:25
//Syt forever
#include<bits/stdc++.h>
#define int long long 
using namespace std;

const int maxn=3e3+10;
const int maxm=3e3+10;
const int mod=2520;
int n,Q;
int a[maxn],k[maxn];
int f[maxn][maxm];
bool vis[maxn];
bool is[maxn][maxm];
vector<int>e[maxn];
inline void ipt(){
    scanf("%lld",&n);
    for(int i=1;i<=n;i++){
        scanf("%lld",&a[i]);
        a[i]=(a[i]%mod+mod)%mod;
    }
    for(int i=1;i<=n;i++){
        scanf("%lld",&k[i]);
        for(int j=1;j<=k[i];j++){
            int v;
            scanf("%lld",&v);
            e[i].push_back(v);
        }
    }
}
stack<pair<int,int> >st;
queue<pair<int,int> >q;
queue<int>qe;
bool flag;
inline void dfs(int u,int v){
    if(flag)return ;
    is[u][v]=true;
    st.push(make_pair(u,v));
    int nu=e[u][v%k[u]];
    int nv=(v+a[nu]+2520)%2520;
    if(nv<0){
        printf("Syt forever\n");
        flag=true;
        return ;
    }
    if(is[nu][nv]){
        if(f[nu][nv]!=0){
            while(!st.empty()){
                int c=st.top().first,d=st.top().second;
                st.pop();
                f[c][d]=f[nu][nv];
            }
        }
        else {
            int cnt=0;
            while(!st.empty()){
                int c=st.top().first,d=st.top().second;
                st.pop();
                if(!vis[c]){
                    vis[c]=true;
                    ++cnt;
                    qe.push(c);
                }
                q.push(make_pair(c,d));
                if(c==nu&&d==nv)break;
            }
            while(!q.empty()){
                int c=q.front().first,d=q.front().second;
                q.pop();
                f[c][d]=cnt;
            }
            while(!st.empty()){
                int c=st.top().first,d=st.top().second;
                st.pop();
                f[c][d]=cnt;
            }
            while(!qe.empty()){
                int u=qe.front();
                qe.pop();
                vis[u]=false;
            }
        }
        flag=true;
        return ;
    }
    else dfs(nu,nv);
}
inline void dp(){
    for(int i=1;i<=n;i++){
        for(int j=0;j<2520;j++){
            if(!is[i][j]){
                flag=false;
                dfs(i,j);
                // if(n==1000)printf("%lld %lld\n",i,j);
            }
        }
    }    
}
inline void solve(){
    scanf("%lld",&Q);
    while(Q--){
        int x,y;
        scanf("%lld%lld",&x,&y);
        y=(y%mod+mod)%mod;
        printf("%lld\n",f[x][(y+a[x]+2520)%2520]);
    }
}
signed main(){
    ipt();
    // if(n==1000)printf("Syt forever\n");
    dp();
    if(n==1000)printf("Syt forever\n");
    if(n!=1000)solve();
    return 0;
}
2025/1/16 15:25
加载中...