1 solutions
-
0
/* * __----~~~~~~~~~~~------___ * . . ~~//====...... __--~ ~~ * -. \_|// |||\\ ~~~~~~::::... /~ * ___-==_ _-~o~ \/ ||| \\ _/~~- * __---~~~.==~||\=_ -_--~/_-~|- |\\ \\ _/~ * _-~~ .=~ | \\-_ '-~7 /- / || \ / * .~ .~ | \\ -_ / /- / || \ / * / ____ / | \\ ~-_/ /|- _/ .|| \ / * |~~ ~~|--~~~~--_ \ ~==-/ | \~--===~~ .\ * ' ~-| /| |-~\~~ __--~~ * |-~~-_/ | | ~\_ _-~ /\ * / \ \__ \/~ \__ * _--~ _/ | .-~~____--~-/ ~~==. * ((->/~ '.|||' -_| ~~-/ , . _|| * -_ ~\ ~~---l__i__i__i--~~_/ * _-~-__ ~) \--______________--~~ * //.-~~~-~_--~- |-------~~~~~~~~ * //.-~~~--\ * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * * 神兽保佑 永无BUG */ /* * @Description: I want to be the weakest vegetable in the world! * @Author: I.Z. */ #include<bits/stdc++.h> using namespace std; #define MOD 998244353 #define speMOD 2933256077ll #define int long long #define pii pair<int,int> #define all(v) v.begin(),v.end() #define pb push_back #define REP(i,b,e) for(int i=(b);i<(int)(e);++i) #define over(x) {cout<<(x)<<endl;return;} #define lowbit(x) ((x)&(-(x))) #define cntbit(x) __builtin_popcount(x) #define deal(v) sort(all(v));v.erase(unique(v.begin(),v.end()),v.end()) #define lbound(v,x) lower_bound(all(v),x)-v.begin() mt19937 sd(random_device{}()); int qpow(int a,int b,int m=MOD,int res=1){ a%=m; while(b>0)res=(b&1)?(res*a%m):(res),a=a*a%m,b>>=1; return res; } int exgcd(int x,int y,int &a,int &b){ if(y==0){ a=1;b=0; return x; } int d=exgcd(y,x%y,a,b); int z=b; b=a-b*(x/y); a=z; return d; } int _x_,_y_; inline int inverse(int x,int y=MOD){ exgcd(x,y,_x_,_y_); return (_x_+y)%y; } int fac[2000005],inv[2000005]; void init(int n){ fac[0]=inv[0]=1; REP(i,1,n+1)fac[i]=fac[i-1]*i%MOD; inv[n]=qpow(fac[n],MOD-2); for(int i=n-1;i>=1;--i)inv[i]=inv[i+1]*(i+1)%MOD; } int binom(int x,int y){ return x<y||y<0? 0:fac[x]*inv[y]%MOD*inv[x-y]%MOD; } struct ds{ signed rt[200005]; int seg[8000005]; signed ls[8000005],rs[8000005]; int sum[400005]; int tot; void upd(int pos,int l,int r,signed &p,int &val){ if(!p){ p=tot++,ls[p]=rs[p]=seg[p]=0; } if(l==r){ swap(val,seg[p]); return; } int m=(l+r)>>1; if(m>=pos)upd(pos,l,m,ls[p],val); else upd(pos,m+1,r,rs[p],val); } void merge(int l,int r,int p,int p2){ if(l==r){ seg[p]+=seg[p2]; if(seg[p]>=MOD)seg[p]-=MOD; return; } int m=(l+r)>>1; if(!ls[p])ls[p]=ls[p2]; else if(ls[p2])merge(l,m,ls[p],ls[p2]); if(!rs[p])rs[p]=rs[p2]; else if(rs[p2])merge(m+1,r,rs[p],rs[p2]); } void init(vector<int>t,int spe){ tot=1;t.pb(spe); for(auto i:t)rt[i]=sum[i]=0; } }seg; int n,m,K; vector<int>v[1000005]; vector<int>a[1000005]; map<int,int>mp[1000005]; int tot; int ed[1000005]; int fl[1000005]; int f[2000005],g[1000005],tp[1000005]; vector<int>add[1000005]; vector<int>vt[1000005]; int X; void dfs(int x){ for(auto i:vt[x])dfs(i); seg.sum[x]=x<tot? f[x]:0; for(auto i:vt[x]){ if(!seg.rt[x])seg.rt[x]=seg.rt[i]; else if(seg.rt[i])seg.merge(0,m-1,seg.rt[x],seg.rt[i]); (seg.sum[x]+=seg.sum[i])%=MOD; } if(x<tot){ for(auto [j,i]:mp[x]){ int res=MOD-seg.sum[x]; int p=lbound(v[X],j); if(p<v[X].size()&&v[X][p]==j){ seg.upd(p,0,m-1,seg.rt[x],res); (res+=seg.sum[x])%=MOD; f[i]+=res; } } }else{ REP(i,0,m){ int res=0; seg.upd(i,0,m-1,seg.rt[x],res); (res+=seg.sum[x])%=MOD; if(mp[0].find(v[X][i])==mp[0].end())g[v[X][i]]+=res; else f[mp[0][v[X][i]]]+=res; } } } int yes[100005]; void Main() { cin>>n>>m>>K; REP(i,0,n)v[i].clear(),yes[i]=0; REP(i,0,m){ int x,y; cin>>x>>y; --x,--y; v[x].pb(y); } m=K; REP(i,0,n)add[i].clear(); REP(i,0,tot)mp[i].clear(); tot=1; REP(i,0,m){ int p;cin>>p; vector<int>t(p,0); for(auto &j:t)cin>>j,--j; if(p==1)yes[t[0]]=1; int c=0; for(auto j:t){ if(mp[c].find(j)==mp[c].end()){ add[j].pb(tot);tp[tot]=j; mp[c][j]=tot++; ed[tot-1]=0; } c=mp[c][j]; } ed[c]=1; } fl[0]=-1; queue<int>q; q.push(0); while(!q.empty()){ int x=q.front();q.pop(); for(auto [j,i]:mp[x]){ int y=x; while(y&&(y==x||mp[y].find(j)==mp[y].end()))y=fl[y]; if(y!=x&&mp[y].find(j)!=mp[y].end())y=mp[y][j]; fl[i]=y; q.push(i); ed[i]|=ed[y]; } } REP(i,0,n+tot)vt[i].clear(); REP(i,0,tot)f[i]=0; REP(i,0,n)g[i]=0; REP(i,1,tot)if(fl[i])vt[fl[i]].pb(i); else vt[tp[i]+tot].pb(i); if(mp[0].find(0)==mp[0].end())g[0]=1; else f[mp[0][0]]=1; REP(i,0,n){ X=i; g[i]%=MOD; if(yes[i])g[i]=0; for(auto j:add[i])f[j]=ed[j]? 0:f[j]%MOD; for(auto j:v[i]){ if(mp[0].find(j)==mp[0].end())g[j]+=g[i]; else f[mp[0][j]]+=g[i]; } seg.init(add[i],i+tot); sort(all(v[i])); m=v[i].size(); dfs(i+tot); } REP(i,1,n){ int ans=g[i]; for(auto j:add[i])ans+=f[j]; cout<<ans%MOD<<' '; } cout<<endl; } void TC() { int tc=1; cin>>tc; while(tc--){ Main(); cout.flush(); } } signed main() { freopen("path.in","r",stdin); freopen("path.out","w",stdout); return cin.tie(0),cout.tie(0),ios::sync_with_stdio(0),TC(),0; } /* 1. CLEAR the arrays (ESPECIALLY multitests) 2. DELETE useless output */
- 1
Information
- ID
- 3064
- Time
- 5000ms
- Memory
- 512MiB
- Difficulty
- 10
- Tags
- (None)
- # Submissions
- 1
- Accepted
- 1
- Uploaded By