#include <bits/stdc++.h>
using namespace std;
#define LCBorz ios_base::sync_with_stdio(false); cin.tie(0);
#define all(x) x.begin(), x.end()
#define endl '\n'
const int N=6005;
const int M=1000005;
const int INF=1e9;
int n,m,l,p;
vector<int> v(N),adj[N],ans(N),t(M,0),t1(M,0);
void dfs(int k,int pa){
for(int j:adj[k]){
if(j==pa)continue;
v[j]+=v[k];
dfs(j,k);
}
}
vector<int> h,h1;
vector<int> rec[N];
void dfs1(int k,int pa){
for(int j:h){
if(l-(v[k]-j)>=0&&t[l-(v[k]-j)]==1){
ans[k]=1;
}
}
h.push_back(v[k]);
for(int j:adj[k]){
if(j==pa)continue;
dfs1(j,k);
}
h.pop_back();
}
void dfs2(int k,int rt){
if(k<=n)rec[rt].push_back(v[k]-v[rt]+p);
for(int j:adj[k]){
dfs2(j,rt);
}
}
void dfs3(int k,int pa){
for(int j:rec[k]){
t1[j]++;
}
int tmp=l-v[k];
for(int i=1;i*i<=tmp;i++){
if(tmp%i==0){
int a=tmp/i;
if(t1[i]||t1[a]){
ans[k]=1;
}
}
}
for(int j:adj[k]){
if(j==pa)continue;
dfs3(j,k);
}
for(int j:rec[k]){
t1[j]--;
}
}
int32_t main() {
LCBorz;
cin>>n>>m>>l>>p;
p++;
for(int i=1;i<=n+m;i++){
int a,b;cin>>a>>b;
v[i]=b+1;
adj[a].push_back(i);
}
dfs(0,0);
for(int i=0;i<=n;i++){
if(v[i]+p<M&&v[i]+p>=0)t[v[i]+p]=1;
}
for(int i=n+1;i<=n+m;i++){
ans[i]|=(v[i]==l);
}
dfs1(0,0);
for(int i=0;i<=n+m;i++){
dfs2(i,i);
}
dfs3(0,0);
for(int i=n+1;i<=n+m;i++){
cout<<(ans[i]?"YES":"NO")<<endl;
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
8536 KB |
Output is correct |
2 |
Correct |
3 ms |
8536 KB |
Output is correct |
3 |
Correct |
6 ms |
8540 KB |
Output is correct |
4 |
Correct |
5 ms |
8796 KB |
Output is correct |
5 |
Correct |
8 ms |
8540 KB |
Output is correct |
6 |
Correct |
8 ms |
8540 KB |
Output is correct |
7 |
Correct |
8 ms |
9304 KB |
Output is correct |
8 |
Correct |
6 ms |
8540 KB |
Output is correct |
9 |
Correct |
5 ms |
8540 KB |
Output is correct |
10 |
Correct |
3 ms |
8536 KB |
Output is correct |
11 |
Correct |
6 ms |
8792 KB |
Output is correct |
12 |
Correct |
6 ms |
9052 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
8792 KB |
Output is correct |
2 |
Correct |
18 ms |
8796 KB |
Output is correct |
3 |
Correct |
19 ms |
8888 KB |
Output is correct |
4 |
Correct |
20 ms |
8796 KB |
Output is correct |
5 |
Correct |
98 ms |
28244 KB |
Output is correct |
6 |
Correct |
95 ms |
27988 KB |
Output is correct |
7 |
Correct |
70 ms |
19548 KB |
Output is correct |
8 |
Correct |
73 ms |
19520 KB |
Output is correct |
9 |
Correct |
19 ms |
8792 KB |
Output is correct |
10 |
Correct |
16 ms |
8792 KB |
Output is correct |
11 |
Correct |
23 ms |
9304 KB |
Output is correct |
12 |
Correct |
84 ms |
28076 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
8536 KB |
Output is correct |
2 |
Correct |
3 ms |
8536 KB |
Output is correct |
3 |
Correct |
6 ms |
8540 KB |
Output is correct |
4 |
Correct |
5 ms |
8796 KB |
Output is correct |
5 |
Correct |
8 ms |
8540 KB |
Output is correct |
6 |
Correct |
8 ms |
8540 KB |
Output is correct |
7 |
Correct |
8 ms |
9304 KB |
Output is correct |
8 |
Correct |
6 ms |
8540 KB |
Output is correct |
9 |
Correct |
5 ms |
8540 KB |
Output is correct |
10 |
Correct |
3 ms |
8536 KB |
Output is correct |
11 |
Correct |
6 ms |
8792 KB |
Output is correct |
12 |
Correct |
6 ms |
9052 KB |
Output is correct |
13 |
Correct |
18 ms |
8792 KB |
Output is correct |
14 |
Correct |
18 ms |
8796 KB |
Output is correct |
15 |
Correct |
19 ms |
8888 KB |
Output is correct |
16 |
Correct |
20 ms |
8796 KB |
Output is correct |
17 |
Correct |
98 ms |
28244 KB |
Output is correct |
18 |
Correct |
95 ms |
27988 KB |
Output is correct |
19 |
Correct |
70 ms |
19548 KB |
Output is correct |
20 |
Correct |
73 ms |
19520 KB |
Output is correct |
21 |
Correct |
19 ms |
8792 KB |
Output is correct |
22 |
Correct |
16 ms |
8792 KB |
Output is correct |
23 |
Correct |
23 ms |
9304 KB |
Output is correct |
24 |
Correct |
84 ms |
28076 KB |
Output is correct |
25 |
Correct |
18 ms |
8792 KB |
Output is correct |
26 |
Correct |
17 ms |
8792 KB |
Output is correct |
27 |
Correct |
17 ms |
8796 KB |
Output is correct |
28 |
Correct |
18 ms |
8796 KB |
Output is correct |
29 |
Correct |
94 ms |
27948 KB |
Output is correct |
30 |
Correct |
99 ms |
28240 KB |
Output is correct |
31 |
Correct |
74 ms |
19544 KB |
Output is correct |
32 |
Correct |
70 ms |
19408 KB |
Output is correct |
33 |
Correct |
16 ms |
8796 KB |
Output is correct |
34 |
Correct |
14 ms |
8796 KB |
Output is correct |
35 |
Correct |
25 ms |
9556 KB |
Output is correct |
36 |
Correct |
102 ms |
33108 KB |
Output is correct |