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 <iostream>
#include <set>
#include <vector>
using namespace std;
const int N = 3009;
bool bun[N];
int pf[N], p[N], l[N], lf[N], h[N];
vector < int > v[N], frz[N], lungimi[N];
set < int > lengths;
int frecv[(int)1e6 + 7];
int s, k;
void dfsb(int nod = 0) {
lungimi[nod].push_back(s + l[nod]);
for (int i : v[nod]) {
dfsb(i);
for (int j : lungimi[i])
lungimi[nod].push_back(j + l[nod]);
}
for (int i : frz[nod]) {
lungimi[nod].push_back(l[nod] + lf[i] + s);
}
}
void dfslen(int nod = 0, int lgth = 0) {
lengths.insert(lgth + s);
for (int i : v[nod])
dfslen(i, lgth + l[i]);
for (int i : frz[nod])
h[i] = lgth + lf[i];
}
void plimba(int nod = 0) {
for (int i : lungimi[nod])
frecv[i]++;
for (int i : v[nod])
plimba(i);
for (int i : frz[nod]) {
if (k - h[i] < 0)
continue;
int a(1);
while (a * a <= k - h[i]) {
if ((frecv[a] || frecv[(k - h[i]) / a]) && (k - h[i]) % a == 0)
bun[i] = 1;
++a;
}
}
for (int i : lungimi[nod])
frecv[i]--;
}
int main()
{
int n, m;
cin >> n >> m >> k >> s;
++s;
for (int i = 1; i <= n; ++i) {
cin >> p[i] >> l[i];
l[i]++;
v[p[i]].push_back(i);
}
for (int i = 0; i < m; ++i) {
cin >> pf[i] >> lf[i];
lf[i]++;
frz[pf[i]].push_back(i);
}
dfslen();
for (int i = 0; i < m; ++i) {
int curl(s + lf[i]), curn = pf[i];
while (curn != 0) {
if (lengths.find(k - curl) != lengths.end())
bun[i] = 1;
curl += l[curn];
curn = p[curn];
}
}
dfsb();
plimba();
for (int i = 0; i < m; ++i) {
if (bun[i])
cout << "YES\n";
else
cout << "NO\n";
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |