#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define rrep(i, n) for (int i = (int)(n) - 1; i >= 0; --i)
#define rep2(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define all(v) begin(v), end(v)
using ll = long long;
struct edge {
int to;
ll cost;
edge(int t, ll c) : to(t), cost(c) {}
};
int main() {
int n, m, k, s; cin >> n >> m >> k >> s;
++n;
vector<vector<edge>> g(n);
vector<int> par(n, -1);
++s;
rep2 (i, 1, n) {
int p, l; cin >> p >> l;
g[p].emplace_back(i, l + 1);
par[i] = p;
}
vector<ll> d(n);
vector<vector<ll>> ch(n);
{
auto dfs = [&](auto&& self, int v) -> vector<ll> {
vector<ll> res;
for (auto e : g[v]) {
d[e.to] = d[v] + e.cost;
for (auto i : self(self, e.to)) res.push_back(i + e.cost);
}
res.push_back(0);
return ch[v] = res;
};
dfs(dfs, 0);
}
set<int> exist;
rep (i, n) {
if (d[i] <= 3 * k) exist.insert(d[i]);
}
vector<vector<pair<int, int>>> qs(n);
vector<bool> ans(m, false);
rep (i, m) {
int p, l; cin >> p >> l;
int t = k - l - 1;
if (d[p] == t) ans[i] = true;
else {
t -= d[p];
qs[p].emplace_back(i, t);
for (int j = p; j != -1; j = par[j]) {
if (exist.count(t + d[j] - s) != 0) ans[i] = true;
}
}
}
{
vector<int> cnt(k + 1);
auto dfs = [&](auto&& self, int v) -> void {
for (auto i : ch[v]) {
if (i <= k) ++cnt[i];
}
for (auto [i, t] : qs[v]) {
for (ll j = 1; j * j <= t; ++j) {
if (t % j != 0) continue;
if (s <= j && cnt[j - s] > 0) ans[i] = true;
if (s <= t / j && cnt[t / j - s] > 0) ans[i] = true;
}
}
for (auto e : g[v]) self(self, e.to);
for (auto i : ch[v]) {
if (i <= k) --cnt[i];
}
};
dfs(dfs, 0);
}
for (auto i : ans) puts(i ? "YES" : "NO");
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
860 KB |
Output is correct |
4 |
Correct |
4 ms |
1372 KB |
Output is correct |
5 |
Correct |
5 ms |
4444 KB |
Output is correct |
6 |
Correct |
4 ms |
4288 KB |
Output is correct |
7 |
Correct |
8 ms |
5468 KB |
Output is correct |
8 |
Correct |
3 ms |
4444 KB |
Output is correct |
9 |
Correct |
3 ms |
4444 KB |
Output is correct |
10 |
Correct |
0 ms |
600 KB |
Output is correct |
11 |
Correct |
2 ms |
3932 KB |
Output is correct |
12 |
Correct |
5 ms |
4536 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
4956 KB |
Output is correct |
2 |
Correct |
12 ms |
4920 KB |
Output is correct |
3 |
Correct |
11 ms |
4988 KB |
Output is correct |
4 |
Correct |
11 ms |
4956 KB |
Output is correct |
5 |
Correct |
150 ms |
33628 KB |
Output is correct |
6 |
Correct |
153 ms |
33496 KB |
Output is correct |
7 |
Correct |
86 ms |
19468 KB |
Output is correct |
8 |
Correct |
75 ms |
19532 KB |
Output is correct |
9 |
Correct |
15 ms |
4956 KB |
Output is correct |
10 |
Correct |
10 ms |
4956 KB |
Output is correct |
11 |
Correct |
10 ms |
5208 KB |
Output is correct |
12 |
Correct |
207 ms |
32712 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
860 KB |
Output is correct |
4 |
Correct |
4 ms |
1372 KB |
Output is correct |
5 |
Correct |
5 ms |
4444 KB |
Output is correct |
6 |
Correct |
4 ms |
4288 KB |
Output is correct |
7 |
Correct |
8 ms |
5468 KB |
Output is correct |
8 |
Correct |
3 ms |
4444 KB |
Output is correct |
9 |
Correct |
3 ms |
4444 KB |
Output is correct |
10 |
Correct |
0 ms |
600 KB |
Output is correct |
11 |
Correct |
2 ms |
3932 KB |
Output is correct |
12 |
Correct |
5 ms |
4536 KB |
Output is correct |
13 |
Correct |
11 ms |
4956 KB |
Output is correct |
14 |
Correct |
12 ms |
4920 KB |
Output is correct |
15 |
Correct |
11 ms |
4988 KB |
Output is correct |
16 |
Correct |
11 ms |
4956 KB |
Output is correct |
17 |
Correct |
150 ms |
33628 KB |
Output is correct |
18 |
Correct |
153 ms |
33496 KB |
Output is correct |
19 |
Correct |
86 ms |
19468 KB |
Output is correct |
20 |
Correct |
75 ms |
19532 KB |
Output is correct |
21 |
Correct |
15 ms |
4956 KB |
Output is correct |
22 |
Correct |
10 ms |
4956 KB |
Output is correct |
23 |
Correct |
10 ms |
5208 KB |
Output is correct |
24 |
Correct |
207 ms |
32712 KB |
Output is correct |
25 |
Correct |
10 ms |
4956 KB |
Output is correct |
26 |
Correct |
10 ms |
4956 KB |
Output is correct |
27 |
Correct |
10 ms |
4956 KB |
Output is correct |
28 |
Correct |
10 ms |
4952 KB |
Output is correct |
29 |
Correct |
135 ms |
33452 KB |
Output is correct |
30 |
Correct |
130 ms |
33532 KB |
Output is correct |
31 |
Correct |
79 ms |
19540 KB |
Output is correct |
32 |
Correct |
78 ms |
19284 KB |
Output is correct |
33 |
Correct |
10 ms |
4952 KB |
Output is correct |
34 |
Correct |
8 ms |
4956 KB |
Output is correct |
35 |
Correct |
12 ms |
5468 KB |
Output is correct |
36 |
Correct |
239 ms |
40356 KB |
Output is correct |