#include <bits/stdc++.h>
#define pb push_back
#define F first
#define S second
#define all(x) x.begin(), x.end()
using namespace std;
typedef int ll;
typedef long double ld;
typedef string str;
typedef pair<ll, ll> pll;
const ll Mod = 1e9 + 7;
const int Maxn = 3e3 + 10;
const ll Inf = 1000000000000000LL;
vector<pll> G[Maxn];
vector<pll> Q[Maxn];
vector<ll> R[Maxn];
bool ans[Maxn];
ll dep[Maxn];
map<ll, ll> mp;
void DFS(ll u, ll d = 0){
dep[u] = d;
R[u].pb(0);
mp[d] = 1;
for(auto E : G[u]){
ll adj = E.F;
DFS(adj, d + E.S);
for(auto x : R[adj]) R[u].pb(x + E.S);
}
sort(all(R[u]));
R[u].resize(unique(all(R[u])) - R[u].begin());
R[u].shrink_to_fit();
}
ll SS;
ll cnt[1000010];
bool check(ll x){
if(x < 0) return false;
if(x == 0) return true;
assert(x != 0);
return (cnt[x] > 0);
}
ll n;
ll in[Maxn];
map<ll, ll> mp2;
void DFS2(ll u){
for(auto x: R[u]) cnt[x] ++;
in[u] = 1;
for(auto E : G[u]){
DFS2(E.F);
}
for(auto X : Q[u]){
ll x = X.F - dep[u];
mp2.clear();
for(int i = 0; i <= n; i++){
if(in[i] == 1) mp2[dep[u] - dep[i]] = 1;
}
for(int i = 0; i <= n; i++){
if(mp2.count(X.F - dep[i] - SS - 1)) ans[X.S] = true;
}
//cerr << u << ' ' << X.F << ' ' << x << '\n';
//if(x >= s + 1) ans[X.S] = (cnt[x - (s + 1)] > 0);
for(int i = 1; i <= 1000; i++){
if(x % i == 0){
ans[X.S] |= check(i - (SS + 1));
ans[X.S] |= check((x / i) - (SS + 1));
}
}
if(x == 0) ans[X.S] = true;
}
in[u] = 0;
for(auto x : R[u]) cnt[x] --;
}
int main(){
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
ll m, k;
cin >> n >> m >> k >> SS;
//assert(k > 10);
ll p, l;
for(int i = 1; i <= n; i++){
cin >> p >> l;
G[p].pb({i, l + 1});
}
for(int i = 1; i <= m; i++){
cin >> p >> l;
//assert(l < k-1);
//assert(p != 0);
//if(p == 0){
// ans[i] = (( (k - l - 1) % (s + 1) ) == 0);
// }
Q[p].pb({k - l - 1, i});
}
//cerr << '\n';
DFS(0);
DFS2(0);
for(int i = 1; i <= m; i++) cout << (ans[i] ? "YES\n" : "NO\n");
return 0;
}
/*
2 4 22
2
0 1
1 5
2 13
2 10
1 4
0 7
*/
Compilation message
fil.cpp:17:20: warning: overflow in implicit constant conversion [-Woverflow]
const ll Inf = 1000000000000000LL;
^~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
632 KB |
Output is correct |
2 |
Correct |
6 ms |
632 KB |
Output is correct |
3 |
Correct |
8 ms |
1016 KB |
Output is correct |
4 |
Correct |
18 ms |
1276 KB |
Output is correct |
5 |
Correct |
18 ms |
4600 KB |
Output is correct |
6 |
Correct |
7 ms |
3548 KB |
Output is correct |
7 |
Correct |
29 ms |
4984 KB |
Output is correct |
8 |
Correct |
11 ms |
3960 KB |
Output is correct |
9 |
Correct |
10 ms |
3960 KB |
Output is correct |
10 |
Correct |
2 ms |
632 KB |
Output is correct |
11 |
Correct |
8 ms |
632 KB |
Output is correct |
12 |
Correct |
16 ms |
1272 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
115 ms |
4928 KB |
Output is correct |
2 |
Correct |
106 ms |
4856 KB |
Output is correct |
3 |
Correct |
104 ms |
4828 KB |
Output is correct |
4 |
Correct |
105 ms |
4856 KB |
Output is correct |
5 |
Correct |
705 ms |
19320 KB |
Output is correct |
6 |
Correct |
713 ms |
19320 KB |
Output is correct |
7 |
Correct |
406 ms |
12280 KB |
Output is correct |
8 |
Correct |
405 ms |
12280 KB |
Output is correct |
9 |
Correct |
155 ms |
5056 KB |
Output is correct |
10 |
Correct |
81 ms |
4988 KB |
Output is correct |
11 |
Correct |
147 ms |
1272 KB |
Output is correct |
12 |
Correct |
771 ms |
16672 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
632 KB |
Output is correct |
2 |
Correct |
6 ms |
632 KB |
Output is correct |
3 |
Correct |
8 ms |
1016 KB |
Output is correct |
4 |
Correct |
18 ms |
1276 KB |
Output is correct |
5 |
Correct |
18 ms |
4600 KB |
Output is correct |
6 |
Correct |
7 ms |
3548 KB |
Output is correct |
7 |
Correct |
29 ms |
4984 KB |
Output is correct |
8 |
Correct |
11 ms |
3960 KB |
Output is correct |
9 |
Correct |
10 ms |
3960 KB |
Output is correct |
10 |
Correct |
2 ms |
632 KB |
Output is correct |
11 |
Correct |
8 ms |
632 KB |
Output is correct |
12 |
Correct |
16 ms |
1272 KB |
Output is correct |
13 |
Correct |
115 ms |
4928 KB |
Output is correct |
14 |
Correct |
106 ms |
4856 KB |
Output is correct |
15 |
Correct |
104 ms |
4828 KB |
Output is correct |
16 |
Correct |
105 ms |
4856 KB |
Output is correct |
17 |
Correct |
705 ms |
19320 KB |
Output is correct |
18 |
Correct |
713 ms |
19320 KB |
Output is correct |
19 |
Correct |
406 ms |
12280 KB |
Output is correct |
20 |
Correct |
405 ms |
12280 KB |
Output is correct |
21 |
Correct |
155 ms |
5056 KB |
Output is correct |
22 |
Correct |
81 ms |
4988 KB |
Output is correct |
23 |
Correct |
147 ms |
1272 KB |
Output is correct |
24 |
Correct |
771 ms |
16672 KB |
Output is correct |
25 |
Correct |
110 ms |
4828 KB |
Output is correct |
26 |
Correct |
107 ms |
4836 KB |
Output is correct |
27 |
Correct |
107 ms |
4828 KB |
Output is correct |
28 |
Correct |
106 ms |
4992 KB |
Output is correct |
29 |
Correct |
726 ms |
19192 KB |
Output is correct |
30 |
Correct |
726 ms |
19320 KB |
Output is correct |
31 |
Correct |
387 ms |
12368 KB |
Output is correct |
32 |
Correct |
390 ms |
12152 KB |
Output is correct |
33 |
Correct |
88 ms |
4856 KB |
Output is correct |
34 |
Correct |
81 ms |
4828 KB |
Output is correct |
35 |
Correct |
142 ms |
1400 KB |
Output is correct |
36 |
Correct |
934 ms |
19848 KB |
Output is correct |