题解
2023-10-20 19:18:12
发布于:江苏
3阅读
0回复
0点赞
#include<bits/stdc++.h>
using namespace std;
#define LL long long
const int N = 3e5 + 10;
const LL p = 998244353;
int n, m, q,T[N], P[N], idx[N];
LL a[N], V[N];
vector<int> G[N];
int deg[N];
LL K = 1, k[N], t[N];
bool vis[N];
void dfs(int j) {
if (vis[j]) return;
vis[j] = true;
int size = G[j].size();
for (int ptr = size-1; ptr >= 0; --ptr) {
int g = G[j][ptr];
++deg[g], dfs(g);
k[j] = (k[j]*k[g]) % p;
}
}
queue<int> Q;
void topSort() {
for (int j = 1; j <= m; ++j)
if (deg[j] == 0) Q.push(j);
while (!Q.empty()) {
int j = Q.front(); Q.pop();
if (vis[j]) continue;
vis[j] = true;
LL w = 1;
int size = G[j].size();
for (int ptr = size-1; ptr >= 0; --ptr) {
int g = G[j][ptr];
t[g] = (t[g] + (w*t[j])%p) % p;
w = (w*k[g]) % p;
deg[g] -= 1;
if (deg[g] == 0) Q.push(g);
}
}
}
int main() {
cin >> n;
for (int i = 1; i <= n; ++i) scanf("%lld", &a[i]);
cin >> m;
for (int j = 1; j <= m; ++j) {
scanf("%d", &T[j]);
k[j] = 1;
if (T[j] == 1) {
scanf("%d%lld", &P[j], &V[j]);
} else if (T[j] == 2) {
scanf("%lld", &k[j]);
} else if (T[j] == 3) {
int C; scanf("%d", &C);
for (int l = 1; l <= C; ++l) {
int g; scanf("%d", &g);
G[j].push_back(g);
}
}
}
cin >> q;
for (int j = 1; j <= q; ++j) scanf("%d", &idx[j]);
memset(vis, false, sizeof(vis));
for (int j = 1; j <= m; ++j) dfs(j);
LL w = 1;
for (int j = q; j >= 1; --j) {
K = (K*k[idx[j]]) % p;
t[idx[j]] = (t[idx[j]]+w) % p;
w = (w*k[idx[j]]) % p;
}
memset(vis, false, sizeof(vis)), topSort();
for (int i = 1; i <= n; ++i)
a[i] = (a[i]*K) % p;
for (int j = 1; j <= m; ++j) {
if (T[j] != 1) continue;
a[P[j]] = (a[P[j]] + ((t[j]*V[j])%p)) % p;
}
for (int i = 1; i <= n; ++i) printf("%lld ", a[i]);
putchar('\n');
return 0;
}
全部评论 1
很与帮助
1周前 来自 广东
0
有帮助,赞一个