#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn = 2e5 + 5, maxN = 8e5 + 5, m = 43;
int n, M;
vector<int> adj[maxn];
int d[maxn], fa[maxn];
bool vis[maxn];
void dfs(int u) {
vis[u] = true;
if (adj[u].size() == 0) return;
for (int v:adj[u]) if (!vis[v]){
d[v] = d[u] + 1, fa[v] = u;
dfs(v);
}
}
signed main() {
ios::sync_with_stdio(0); cin.tie(0);
cin >> n >> M;
for (int i=1;i<=n-1;i++) {
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
queue<int> Q;
Q.push(1);
fa[1] = -1, d[1] = 0;
dfs(1);
int val[n+1][m];
for (int i=1;i<=n;i++) for (int j=0;j<m;j++) val[i][j] = 1;
for (int i=1;i<=n;i++) cin >> val[i][0];
int q;
cin >> q;
while (q--) {
int t;
cin >> t;
if (t==1) {
int x, D, w;
cin >> x >> D >> w;
int v = x;
for (int dep = d[x]+D; dep>=d[x]-D && dep >= 0; dep--) {
if (fa[v]!=-1 && dep-d[v] + d[x]-d[v] + 2 <= D) v = fa[v];
val[v][dep - d[v]] = val[v][dep - d[v]] * w % M;
}
} else if (t==2) {
int x;
cin >> x;
int ans = 1;
for (int v = x; v != -1 && d[v] >= d[x] - m; v = fa[v]) {
ans = ans * val[v][d[x] - d[v]] % M;
}
cout << ans << endl;
}
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
6748 KB |
Output is correct |
2 |
Correct |
2 ms |
4996 KB |
Output is correct |
3 |
Correct |
2 ms |
4956 KB |
Output is correct |
4 |
Incorrect |
3 ms |
5208 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
6748 KB |
Output is correct |
2 |
Incorrect |
808 ms |
58712 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
6748 KB |
Output is correct |
2 |
Incorrect |
808 ms |
58712 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
6492 KB |
Output is correct |
2 |
Incorrect |
836 ms |
47820 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
6744 KB |
Output is correct |
2 |
Incorrect |
809 ms |
54620 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
6748 KB |
Output is correct |
2 |
Correct |
2 ms |
4996 KB |
Output is correct |
3 |
Correct |
2 ms |
4956 KB |
Output is correct |
4 |
Incorrect |
3 ms |
5208 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |