#include <bits/stdc++.h>
using namespace std;
#define rep(i,s,e) for (ll i = s; i <= e; ++i)
#define rrep(i,s,e) for (ll i = s; i >= e; --i)
#define pb push_back
#define pf push_front
#define fi first
#define se second
#define all(a) a.begin(), a.end()
#define SZ(a) (ll)a.size()
typedef long long ll;
typedef pair<ll, ll> ii;
typedef vector<ii> vii;
typedef vector<ll> vi;
typedef vector<double> vd;
typedef vector<string> vs;
typedef vector<ll> vll;
const ll mx = 3e3;
vi adj[mx+1], files[mx+1];
ll cost[mx+1], costf[mx+1], ans[mx+1], cnt[(int)1e7], s, k, p[mx+1], spec[(int)1e7];
void dfs3 (ll u, ll cur){
if(cur>1e6) return;
spec[cur] = 1;
for (ll v : adj[u]) {
dfs3 (v, cur + cost[v]);
}
}
void dfs2 (ll u, ll cur, ll d) {
if (cur>1e6) return;
cnt[cur] += d;
for (ll v : adj[u]) dfs2 (v, cur+cost[v], d);
}
void dfs (ll u, ll cur) {
if (cur>1e6) return;
dfs2 (u, 0, 1);
for (ll v : adj[u]) dfs (v, cur + cost[v]);
for (ll v : files[u]) {
ll x = k - cur - costf[v];
ll go = u, cur2 = 0;
if (x==0) {
ans[v]=1;
goto out;
}
for (ll i = 1; i*i <= x; ++i) {
if (x%i==0) {
if (i-s>=0 && cnt[i-s]) {
ans[v] = 1;
goto out;
}
if (x/i-s>=0&&cnt[x/i-s]) {
ans[v]=1;
goto out;
}
}
}
while (go>=0) {
if (k-costf[v]-cur2-s<0) break;
if (spec[k-costf[v]-cur2-s]) {ans[v] = 1;goto out;}
cur2 += cost[go];
go = p[go];
}
out:;
}
dfs2 (u, 0, -1);
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
ll n, m; cin >> n >> m >> k >> s;
++s;
p[0] = -1;
rep (i,1,n) {
cin >> p[i] >> cost[i];
++cost[i];
adj[p[i]].pb(i);
}
rep (i,1,m) {
ll pr; cin >> pr >> costf[i];
++costf[i];
files[pr].pb(i);
}
dfs3(0,0);
dfs (0,0);
rep (i,1,m) cout << (ans[i]?"YES":"NO") << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
596 KB |
Output is correct |
3 |
Correct |
2 ms |
1876 KB |
Output is correct |
4 |
Correct |
3 ms |
1876 KB |
Output is correct |
5 |
Correct |
7 ms |
8916 KB |
Output is correct |
6 |
Correct |
4 ms |
4948 KB |
Output is correct |
7 |
Correct |
9 ms |
10324 KB |
Output is correct |
8 |
Correct |
5 ms |
7016 KB |
Output is correct |
9 |
Correct |
5 ms |
7124 KB |
Output is correct |
10 |
Correct |
0 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
2 ms |
1620 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
12764 KB |
Output is correct |
2 |
Correct |
14 ms |
12632 KB |
Output is correct |
3 |
Correct |
12 ms |
12628 KB |
Output is correct |
4 |
Correct |
12 ms |
12756 KB |
Output is correct |
5 |
Correct |
130 ms |
14960 KB |
Output is correct |
6 |
Correct |
139 ms |
15120 KB |
Output is correct |
7 |
Correct |
78 ms |
12772 KB |
Output is correct |
8 |
Correct |
76 ms |
12840 KB |
Output is correct |
9 |
Correct |
14 ms |
12616 KB |
Output is correct |
10 |
Correct |
13 ms |
12244 KB |
Output is correct |
11 |
Correct |
5 ms |
944 KB |
Output is correct |
12 |
Correct |
71 ms |
6268 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
596 KB |
Output is correct |
3 |
Correct |
2 ms |
1876 KB |
Output is correct |
4 |
Correct |
3 ms |
1876 KB |
Output is correct |
5 |
Correct |
7 ms |
8916 KB |
Output is correct |
6 |
Correct |
4 ms |
4948 KB |
Output is correct |
7 |
Correct |
9 ms |
10324 KB |
Output is correct |
8 |
Correct |
5 ms |
7016 KB |
Output is correct |
9 |
Correct |
5 ms |
7124 KB |
Output is correct |
10 |
Correct |
0 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
2 ms |
1620 KB |
Output is correct |
13 |
Correct |
14 ms |
12764 KB |
Output is correct |
14 |
Correct |
14 ms |
12632 KB |
Output is correct |
15 |
Correct |
12 ms |
12628 KB |
Output is correct |
16 |
Correct |
12 ms |
12756 KB |
Output is correct |
17 |
Correct |
130 ms |
14960 KB |
Output is correct |
18 |
Correct |
139 ms |
15120 KB |
Output is correct |
19 |
Correct |
78 ms |
12772 KB |
Output is correct |
20 |
Correct |
76 ms |
12840 KB |
Output is correct |
21 |
Correct |
14 ms |
12616 KB |
Output is correct |
22 |
Correct |
13 ms |
12244 KB |
Output is correct |
23 |
Correct |
5 ms |
944 KB |
Output is correct |
24 |
Correct |
71 ms |
6268 KB |
Output is correct |
25 |
Correct |
12 ms |
12756 KB |
Output is correct |
26 |
Correct |
11 ms |
12640 KB |
Output is correct |
27 |
Correct |
10 ms |
12756 KB |
Output is correct |
28 |
Correct |
12 ms |
12584 KB |
Output is correct |
29 |
Correct |
143 ms |
15308 KB |
Output is correct |
30 |
Correct |
138 ms |
15132 KB |
Output is correct |
31 |
Correct |
67 ms |
12764 KB |
Output is correct |
32 |
Correct |
71 ms |
12804 KB |
Output is correct |
33 |
Correct |
12 ms |
12576 KB |
Output is correct |
34 |
Correct |
11 ms |
12244 KB |
Output is correct |
35 |
Correct |
5 ms |
884 KB |
Output is correct |
36 |
Correct |
93 ms |
3508 KB |
Output is correct |