TLE on #10,95pts求助

P7077 [CSP-S2020] 函数调用

Cry_For_theMoon @ 2020-11-17 20:16:11

考场上本人nc暴力都没写,赛后补题(结果发现其实水的一批)

不知道为什么第10个点TLE了,其他点都是飞快,我觉得是哪里RE报TLE但是调不出来

蒟蒻求助QwQ

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAXN=1e6+10,MAXM=2e6+10,MODER=998244353;
inline long long read(){
    long long x=0,sign=0; char s=getchar();
    while(s>'9'||s<'0')sign|=s=='-',s=getchar();
    while(s<='9'&&s>='0')x=(x<<1)+(x<<3)+s-'0',s=getchar();
    return sign?-x:x;
}
int n,m,t[MAXN],p[MAXN],v[MAXN],q,incnt[MAXN]; 
long long a[MAXN];
struct Edge{
    int u,v;
}edge[MAXM];
int first[MAXN],next[MAXM],tot;
long long mult[MAXN],f[MAXN];
inline void addedge(int u,int v){
    edge[++tot].u=u;edge[tot].v=v;
    next[tot]=first[u];first[u]=tot;
}
void dfs(int u){
    mult[u]=1;
    if(t[u]==1){
        return;
    }
    if(t[u]==2){
        mult[u]=v[u];
        return;
    }
    for(int j=first[u];j;j=next[j]){
        int v = edge[j].v;
        if(!mult[v]){
            dfs(v);
        }
        mult[u] = mult[u]*mult[v]%MODER;
    }
}
int qu[MAXN],head,rear; 
int main(){
    n=read();
    for(int i=1;i<=n;i++){
        a[i] = read();
    }
    m=read();
    for(int i=1;i<=m;i++){
        t[i] = read();
        if(t[i]==1){
            p[i] = read();v[i] = read();
        }else if(t[i]==2){
            v[i] = read();
        }else{
            int cnt=0;cnt = read();
            for(int j=1,vi;j<=cnt;j++){
                vi = read();
                addedge(i,vi);
                incnt[vi]++;
            }
        }
    }
    q = read();
    for(int i=1,vi;i<=q;i++){
        vi = read();
        addedge(m+1,vi);
        incnt[vi]++;
    }
    t[m+1]=3; 
    dfs(m+1); 
    long long x = mult[m+1];
    for(int i=1;i<=n;i++){
        a[i] = a[i] * x %MODER;
    }
    long long prod = 1;
    head=1,rear=1;
    for(int j=first[m+1];j;j=next[j]){
        int vi = edge[j].v;
        f[vi] = (f[vi] + prod)%MODER;
        prod = prod*mult[vi]%MODER;
        incnt[vi]--;
    }
    for(int i=1;i<=m;i++){
        if(!incnt[i]){
            qu[rear++] = i;
        }
    }
    while(head < rear){
        int u = qu[head++];
        prod = f[u];
        if(t[u]==1){
            a[p[u]] = (a[p[u]]+f[u]*v[u]%MODER)%MODER;
            continue;
        }
        for(int j=first[u];j;j=next[j]){
            int vi = edge[j].v;
            f[vi] = (f[vi]+prod)%MODER;
            prod = prod*mult[vi]%MODER;
            incnt[vi]--;
            if(!incnt[vi]){
                qu[rear++] = vi;
            }
        }
    }
    for(int i=1;i<=n;i++){
        printf("%lld ",a[i]%MODER);
    }

    return 0;
} 

by Cry_For_theMoon @ 2020-11-17 20:25:48

系数有0,记忆化挂掉了

此贴终


by Loliconrz @ 2020-11-17 22:28:22

神仙妹妹 /se


by Haphyxlos @ 2020-11-18 18:55:29

神仙妹妹 /se


by Haphyxlos @ 2023-11-16 09:00:38

@Cry_For_theMoon 今天写这题的时候踩坑了,难绷


|