#include <bits/stdc++.h>
// #pragma GCC optimize ("Ofast,unroll-loops")
// #pragma GCC target ("avx2")
using namespace std;
typedef long long ll;
typedef pair<int, int> pp;
#define er(args ...) cerr << __LINE__ << ": ", err(new istringstream(string(#args)), args), cerr << endl
#define per(i,r,l) for(int i = (r); i >= (l); i--)
#define rep(i,l,r) for(int i = (l); i < (r); i++)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
#define pb push_back
#define ss second
#define ff first
void err(istringstream *iss){}template<typename T,typename ...Args> void err(istringstream *iss,const T &_val, const Args&...args){string _name;*iss>>_name;if(_name.back()==',')_name.pop_back();cerr<<_name<<" = "<<_val<<", ",err(iss,args...);}
void IOS(){
cin.tie(0) -> sync_with_stdio(0);
// #ifndef ONLINE_JUDGE
// freopen("in.in", "r", stdin);
// freopen("out.out", "w", stdout);
// #endif
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const ll mod = 1e9 + 7, maxn = 6e3 + 5, maxk = 2e6 + 5, lg = 22, inf = ll(1e9) + 5;
ll pw(ll a,ll b,ll md=mod){if(!b)return 1;ll k=pw(a,b>>1ll);return k*k%md*(b&1ll?a:1)%md;}
int l[maxn], par[maxn], n, m, k, s;
vector<int> adj[maxn];
int vl[maxk], vl2[maxk];
bool ans[maxn];
vector<int> res;
void dfs3(int r, int h = s){
if(r > n) return;
res.pb(h);
for(int c: adj[r]) dfs3(c, h + l[c]);
}
void dfs2(int r, int x, int h = 0){
if(r == x || r > n) return;
res.pb(h + s);
for(int c: adj[r]) dfs2(c, x, h + l[c]);
}
void add(int u, int h){
res.clear();
dfs3(u);
for(int c: res) vl[c]++;
res.clear();
dfs2(0, u);
for(ll c: res) if(c > h) vl2[c-h]++;
}
void del(int u, int h){
res.clear();
dfs3(u);
for(int c: res) vl[c]--;
res.clear();
dfs2(0, u);
for(int c: res) if(c > h) vl2[c-h]--;
}
void dfs(int r, int h = 0){
if(r > n){ r -= n;
ll t = k - h;
ans[r] = vl2[t] || t == 0;
if(!ans[r]){
for(int j = 1; j*j <= t; j++){
if(t%j == 0){
if(vl[j] || vl[t/j]){
ans[r] = true;
break;
}
}
}
}
}else{
add(r, h);
for(int c: adj[r]) dfs(c, h + l[c]);
del(r, h);
}
}
int main(){ IOS();
// rep(i,1,maxk) for(int j = i; j < maxk; j += i) dv[j].pb(i);
par[0] = -1;
cin >> n >> m >> k >> s; s++;
rep(i,1,n+m+1) cin >> par[i] >> l[i], adj[par[i]].pb(i), l[i]++;
dfs(0);
rep(i,1,m+1) cout << (ans[i]?"YES\n":"NO\n");
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
4 ms |
468 KB |
Output is correct |
3 |
Correct |
7 ms |
1236 KB |
Output is correct |
4 |
Correct |
6 ms |
1364 KB |
Output is correct |
5 |
Correct |
12 ms |
8496 KB |
Output is correct |
6 |
Correct |
12 ms |
7780 KB |
Output is correct |
7 |
Correct |
11 ms |
8712 KB |
Output is correct |
8 |
Correct |
12 ms |
7504 KB |
Output is correct |
9 |
Correct |
9 ms |
7508 KB |
Output is correct |
10 |
Correct |
0 ms |
468 KB |
Output is correct |
11 |
Correct |
6 ms |
468 KB |
Output is correct |
12 |
Correct |
5 ms |
724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
328 ms |
8468 KB |
Output is correct |
2 |
Correct |
356 ms |
8620 KB |
Output is correct |
3 |
Correct |
326 ms |
8416 KB |
Output is correct |
4 |
Correct |
393 ms |
8412 KB |
Output is correct |
5 |
Correct |
353 ms |
9048 KB |
Output is correct |
6 |
Correct |
335 ms |
8780 KB |
Output is correct |
7 |
Correct |
323 ms |
8908 KB |
Output is correct |
8 |
Correct |
335 ms |
8700 KB |
Output is correct |
9 |
Correct |
345 ms |
8816 KB |
Output is correct |
10 |
Correct |
310 ms |
9320 KB |
Output is correct |
11 |
Correct |
309 ms |
780 KB |
Output is correct |
12 |
Correct |
249 ms |
2248 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
4 ms |
468 KB |
Output is correct |
3 |
Correct |
7 ms |
1236 KB |
Output is correct |
4 |
Correct |
6 ms |
1364 KB |
Output is correct |
5 |
Correct |
12 ms |
8496 KB |
Output is correct |
6 |
Correct |
12 ms |
7780 KB |
Output is correct |
7 |
Correct |
11 ms |
8712 KB |
Output is correct |
8 |
Correct |
12 ms |
7504 KB |
Output is correct |
9 |
Correct |
9 ms |
7508 KB |
Output is correct |
10 |
Correct |
0 ms |
468 KB |
Output is correct |
11 |
Correct |
6 ms |
468 KB |
Output is correct |
12 |
Correct |
5 ms |
724 KB |
Output is correct |
13 |
Correct |
328 ms |
8468 KB |
Output is correct |
14 |
Correct |
356 ms |
8620 KB |
Output is correct |
15 |
Correct |
326 ms |
8416 KB |
Output is correct |
16 |
Correct |
393 ms |
8412 KB |
Output is correct |
17 |
Correct |
353 ms |
9048 KB |
Output is correct |
18 |
Correct |
335 ms |
8780 KB |
Output is correct |
19 |
Correct |
323 ms |
8908 KB |
Output is correct |
20 |
Correct |
335 ms |
8700 KB |
Output is correct |
21 |
Correct |
345 ms |
8816 KB |
Output is correct |
22 |
Correct |
310 ms |
9320 KB |
Output is correct |
23 |
Correct |
309 ms |
780 KB |
Output is correct |
24 |
Correct |
249 ms |
2248 KB |
Output is correct |
25 |
Correct |
317 ms |
8616 KB |
Output is correct |
26 |
Correct |
335 ms |
8612 KB |
Output is correct |
27 |
Correct |
315 ms |
8652 KB |
Output is correct |
28 |
Correct |
320 ms |
8552 KB |
Output is correct |
29 |
Correct |
328 ms |
9012 KB |
Output is correct |
30 |
Correct |
324 ms |
9024 KB |
Output is correct |
31 |
Correct |
333 ms |
8996 KB |
Output is correct |
32 |
Correct |
345 ms |
8960 KB |
Output is correct |
33 |
Correct |
333 ms |
9560 KB |
Output is correct |
34 |
Correct |
347 ms |
9420 KB |
Output is correct |
35 |
Correct |
325 ms |
756 KB |
Output is correct |
36 |
Correct |
310 ms |
1612 KB |
Output is correct |