This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define F first
#define S second
#define pb push_back
#define endl '\n'
using namespace std;
const int N = 3005, K = 1e6+5, X = 5, Y = K/X+100;
int nn, mm, kk, ss;
bool ad[K], ans[N];
int pr[N], wt[N];
ll c1[K], c2[Y], ds[N], pd[K];
vector<ll> a1[N], a2[N];
vector<int> ch[N];
vector<pii> qr[N];
void dfs1(int v, int s, ll w) {
w += wt[v];
if (w < Y) {
a2[s].pb(w);
c2[w]++;
}
for (int i = 2; i <= X; i++)
if (w * i < K) {
a1[s].pb(w * i);
c1[w*i]++;
}
for (int u : ch[v])
dfs1(u, s, w);
}
void dfs2(int v) {
dfs1(v, v, ss-wt[v]);
/*for (int i = 0; i <= kk; i++)
cout << c1[i] << ' ';
cout << endl;*/
for (pii x : qr[v]) {
if (ans[x.S])
continue;
if (c1[kk-x.F])
ans[x.S] = true;
/*else
for (int i = 1; i < Y; i++)
if (c2[i] && (kk-x.F) % i == 0)
ans[x.S] = true;*/
}
for (int u : ch[v])
dfs2(u);
for (ll x : a1[v])
c1[x]--;
for (ll x : a2[v])
c2[x]--;
}
void dfs3(int v, ll w) {
w += wt[v];
ds[v] = w;
for (int u : ch[v])
dfs3(u, w);
}
int main() {
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> nn >> mm >> kk >> ss;
ss++;
pr[0] = -1;
for (int i = 1; i <= nn; i++) {
cin >> pr[i] >> wt[i];
wt[i]++;
ch[pr[i]].pb(i);
}
dfs3(0, 0);
for (int i = 0; i <= nn; i++)
if (ds[i] < K)
ad[ds[i]] = true;
for (int i = 0; i < mm; i++) {
int v, l;
cin >> v >> l;
qr[v].pb({ds[v]+l+1, i});
}
for (int i = 0; i <= nn; i++)
for (pii x : qr[i]) {
if (x.F == kk) {
ans[x.S] = true;
continue;
}
int v = i;
while (v != -1) {
if (kk+ds[v]-x.F-ss >= 0 && ad[kk+ds[v]-x.F-ss]) {
ans[x.S] = true;
break;
}
v = pr[v];
}
}
dfs2(0);
for (int i = 0; i < mm; i++)
cout << (ans[i] ? "YES" : "NO") << endl;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |