#include "bits/stdc++.h"
using namespace std;
void solve() {
int n, m, k, s;
cin >> n >> m >> k >> s;
s++;
vector<int> p(n + 1, -1), l(n + 1, 1);
vector<vector<int>> g(n + 1);
for (int i = 1; i <= n; i++) {
cin >> p[i] >> l[i];
l[i] = l[i] + 1 + l[p[i]];
g[p[i]].push_back(i);
}
unordered_set<int> have;
for (int i = 0; i <= n; i++) {
have.insert(l[i] + s);
}
vector<int> ans(m);
vector<vector<pair<int, int>>> qu(n + 1);
for (int q = 0; q < m; q++) {
int pr, len;
cin >> pr >> len;
len = k - len - l[pr];
bool ok = false;
if (len == 0) {
ok = true;
}
qu[pr].push_back({q, len});
len += l[pr];
int sum = 0;
for (int tmp = pr; tmp != -1; tmp = p[tmp]) {
if (have.find(len - sum) != have.end()) {
ok = true;
}
sum += l[tmp] - (tmp != 0 ? l[p[tmp]] : 0);
}
ans[q] = ok;
}
vector<int> ms((int) 1e6);
auto dfs = [&] (auto&& dfs, int v, int cur) -> void {
if (cur < (int) ms.size()) {
ms[cur]++;
}
for (auto u : g[v]) {
dfs(dfs, u, cur + l[u] - l[v]);
}
};
auto dfs2 = [&] (auto&& dfs2, int v, int cur) -> void {
if (cur < (int) ms.size()) {
ms[cur]--;
}
for (auto u : g[v]) {
dfs2(dfs2, u, cur + l[u] - l[v]);
}
};
auto go = [&] (auto&& go, int v) -> void {
dfs(dfs, v, s);
for (auto [q, x] : qu[v]) {
for (int i = 1; i * i <= x; i++) {
if (x % i == 0) {
if (ms[i] || ms[x / i]) {
ans[q] = 1;
}
}
}
}
for (auto u : g[v]) {
go(go, u);
}
dfs2(dfs2, v, s);
};
go(go, 0);
for (auto ok : ans) {
cout << (ok ? "YES\n" : "NO\n");
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t = 1;
// cin >> t;
while (t--) {
solve();
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4176 KB |
Output is correct |
2 |
Correct |
3 ms |
4432 KB |
Output is correct |
3 |
Correct |
2 ms |
4312 KB |
Output is correct |
4 |
Correct |
4 ms |
4336 KB |
Output is correct |
5 |
Correct |
4 ms |
4432 KB |
Output is correct |
6 |
Correct |
4 ms |
4432 KB |
Output is correct |
7 |
Correct |
7 ms |
4432 KB |
Output is correct |
8 |
Correct |
3 ms |
4432 KB |
Output is correct |
9 |
Correct |
3 ms |
4432 KB |
Output is correct |
10 |
Correct |
2 ms |
4176 KB |
Output is correct |
11 |
Correct |
3 ms |
4432 KB |
Output is correct |
12 |
Correct |
5 ms |
4432 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
4688 KB |
Output is correct |
2 |
Correct |
8 ms |
4688 KB |
Output is correct |
3 |
Correct |
6 ms |
4688 KB |
Output is correct |
4 |
Correct |
7 ms |
4688 KB |
Output is correct |
5 |
Correct |
99 ms |
5004 KB |
Output is correct |
6 |
Correct |
104 ms |
4944 KB |
Output is correct |
7 |
Correct |
62 ms |
4772 KB |
Output is correct |
8 |
Correct |
60 ms |
4944 KB |
Output is correct |
9 |
Correct |
7 ms |
4688 KB |
Output is correct |
10 |
Correct |
6 ms |
4688 KB |
Output is correct |
11 |
Correct |
6 ms |
4824 KB |
Output is correct |
12 |
Correct |
68 ms |
4936 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4176 KB |
Output is correct |
2 |
Correct |
3 ms |
4432 KB |
Output is correct |
3 |
Correct |
2 ms |
4312 KB |
Output is correct |
4 |
Correct |
4 ms |
4336 KB |
Output is correct |
5 |
Correct |
4 ms |
4432 KB |
Output is correct |
6 |
Correct |
4 ms |
4432 KB |
Output is correct |
7 |
Correct |
7 ms |
4432 KB |
Output is correct |
8 |
Correct |
3 ms |
4432 KB |
Output is correct |
9 |
Correct |
3 ms |
4432 KB |
Output is correct |
10 |
Correct |
2 ms |
4176 KB |
Output is correct |
11 |
Correct |
3 ms |
4432 KB |
Output is correct |
12 |
Correct |
5 ms |
4432 KB |
Output is correct |
13 |
Correct |
7 ms |
4688 KB |
Output is correct |
14 |
Correct |
8 ms |
4688 KB |
Output is correct |
15 |
Correct |
6 ms |
4688 KB |
Output is correct |
16 |
Correct |
7 ms |
4688 KB |
Output is correct |
17 |
Correct |
99 ms |
5004 KB |
Output is correct |
18 |
Correct |
104 ms |
4944 KB |
Output is correct |
19 |
Correct |
62 ms |
4772 KB |
Output is correct |
20 |
Correct |
60 ms |
4944 KB |
Output is correct |
21 |
Correct |
7 ms |
4688 KB |
Output is correct |
22 |
Correct |
6 ms |
4688 KB |
Output is correct |
23 |
Correct |
6 ms |
4824 KB |
Output is correct |
24 |
Correct |
68 ms |
4936 KB |
Output is correct |
25 |
Correct |
6 ms |
4688 KB |
Output is correct |
26 |
Correct |
6 ms |
4688 KB |
Output is correct |
27 |
Correct |
6 ms |
4836 KB |
Output is correct |
28 |
Correct |
5 ms |
4688 KB |
Output is correct |
29 |
Correct |
90 ms |
4936 KB |
Output is correct |
30 |
Correct |
86 ms |
4956 KB |
Output is correct |
31 |
Correct |
56 ms |
4776 KB |
Output is correct |
32 |
Correct |
54 ms |
4944 KB |
Output is correct |
33 |
Correct |
6 ms |
4688 KB |
Output is correct |
34 |
Correct |
5 ms |
4688 KB |
Output is correct |
35 |
Correct |
6 ms |
4868 KB |
Output is correct |
36 |
Correct |
112 ms |
5020 KB |
Output is correct |