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>
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 |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |