#include <bits/stdc++.h>
using std::vector;
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr);
int N, M, K, S;
std::cin >> N >> M >> K >> S;
K -= 1, S += 1;
vector<int> parent(N + M + 1), depth(N + M + 1);
vector<vector<int>> children(N + 1);
for (int i = 1; i <= N; ++i) {
int p, l;
std::cin >> p >> l;
parent[i] = p;
depth[i] = depth[p] + l + 1;
children[p].push_back(i);
}
for (int i = N + 1; i <= N + M; ++i) {
int p, l;
std::cin >> p >> l;
parent[i] = p;
depth[i] = depth[p] + l;
children[p].push_back(i);
}
vector<char> exists(K + 1);
vector<vector<int>> list(N + 1);
for (int i = 0; i <= N; ++i) {
exists[depth[i]] = true;
for (int j = i; j != 0;) {
j = parent[j];
const int len = depth[i] - depth[j] + S;
if (len <= K) {
list[j].push_back(len);
}
}
}
vector<char> ans(M);
vector<int> anc, count(K + 1);
auto dfs = [&](auto&& dfs, const int u) -> void {
if (u > N) {
ans[u - (N + 1)] = [&] {
const int goal = K - depth[u];
if (goal % S == 0) {
return true;
}
for (const int v : anc) {
const int shortcut = goal - S + depth[v];
if (shortcut >= 0 and exists[shortcut]) {
return true;
}
}
for (int d = 1; d * d <= goal; ++d) {
if (goal % d == 0) {
if (count[d] > 0 or count[goal / d] > 0) {
return true;
}
}
}
return false;
}();
} else {
anc.push_back(u);
for (const int x : list[u]) {
count[x] += 1;
}
for (const int v : children[u]) {
dfs(dfs, v);
}
anc.pop_back();
for (const int x : list[u]) {
count[x] -= 1;
}
}
};
dfs(dfs, 0);
for (const char c : ans) {
std::cout << (c ? "YES" : "NO") << '\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
852 KB |
Output is correct |
4 |
Correct |
2 ms |
1220 KB |
Output is correct |
5 |
Correct |
4 ms |
5332 KB |
Output is correct |
6 |
Correct |
4 ms |
5204 KB |
Output is correct |
7 |
Correct |
6 ms |
5844 KB |
Output is correct |
8 |
Correct |
3 ms |
5204 KB |
Output is correct |
9 |
Correct |
3 ms |
5204 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
4 ms |
4820 KB |
Output is correct |
12 |
Correct |
4 ms |
5204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
5588 KB |
Output is correct |
2 |
Correct |
8 ms |
5588 KB |
Output is correct |
3 |
Correct |
7 ms |
5588 KB |
Output is correct |
4 |
Correct |
7 ms |
5588 KB |
Output is correct |
5 |
Correct |
82 ms |
25452 KB |
Output is correct |
6 |
Correct |
76 ms |
25376 KB |
Output is correct |
7 |
Correct |
56 ms |
16644 KB |
Output is correct |
8 |
Correct |
54 ms |
16688 KB |
Output is correct |
9 |
Correct |
9 ms |
5584 KB |
Output is correct |
10 |
Correct |
7 ms |
5588 KB |
Output is correct |
11 |
Correct |
6 ms |
5712 KB |
Output is correct |
12 |
Correct |
71 ms |
24316 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
852 KB |
Output is correct |
4 |
Correct |
2 ms |
1220 KB |
Output is correct |
5 |
Correct |
4 ms |
5332 KB |
Output is correct |
6 |
Correct |
4 ms |
5204 KB |
Output is correct |
7 |
Correct |
6 ms |
5844 KB |
Output is correct |
8 |
Correct |
3 ms |
5204 KB |
Output is correct |
9 |
Correct |
3 ms |
5204 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
4 ms |
4820 KB |
Output is correct |
12 |
Correct |
4 ms |
5204 KB |
Output is correct |
13 |
Correct |
8 ms |
5588 KB |
Output is correct |
14 |
Correct |
8 ms |
5588 KB |
Output is correct |
15 |
Correct |
7 ms |
5588 KB |
Output is correct |
16 |
Correct |
7 ms |
5588 KB |
Output is correct |
17 |
Correct |
82 ms |
25452 KB |
Output is correct |
18 |
Correct |
76 ms |
25376 KB |
Output is correct |
19 |
Correct |
56 ms |
16644 KB |
Output is correct |
20 |
Correct |
54 ms |
16688 KB |
Output is correct |
21 |
Correct |
9 ms |
5584 KB |
Output is correct |
22 |
Correct |
7 ms |
5588 KB |
Output is correct |
23 |
Correct |
6 ms |
5712 KB |
Output is correct |
24 |
Correct |
71 ms |
24316 KB |
Output is correct |
25 |
Correct |
8 ms |
5580 KB |
Output is correct |
26 |
Correct |
7 ms |
5580 KB |
Output is correct |
27 |
Correct |
7 ms |
5588 KB |
Output is correct |
28 |
Correct |
7 ms |
5600 KB |
Output is correct |
29 |
Correct |
95 ms |
25280 KB |
Output is correct |
30 |
Correct |
87 ms |
25136 KB |
Output is correct |
31 |
Correct |
45 ms |
16708 KB |
Output is correct |
32 |
Correct |
47 ms |
16396 KB |
Output is correct |
33 |
Correct |
7 ms |
5652 KB |
Output is correct |
34 |
Correct |
8 ms |
5588 KB |
Output is correct |
35 |
Correct |
7 ms |
5972 KB |
Output is correct |
36 |
Correct |
98 ms |
29740 KB |
Output is correct |