1 solutions

  • 0
    @ 2026-7-28 12:00:19
    /*
     *                                                     __----~~~~~~~~~~~------___
     *                                    .  .   ~~//====......          __--~ ~~
     *                    -.            \_|//     |||\\  ~~~~~~::::... /~
     *                 ___-==_       _-~o~  \/    |||  \\            _/~~-
     *         __---~~~.==~||\=_    -_--~/_-~|-   |\\   \\        _/~
     *     _-~~     .=~    |  \\-_    '-~7  /-   /  ||    \      /
     *   .~       .~       |   \\ -_    /  /-   /   ||      \   /
     *  /  ____  /         |     \\ ~-_/  /|- _/   .||       \ /
     *  |~~    ~~|--~~~~--_ \     ~==-/   | \~--===~~        .\
     *           '         ~-|      /|    |-~\~~       __--~~
     *                       |-~~-_/ |    |   ~\_   _-~            /\
     *                            /  \     \__   \/~                \__
     *                        _--~ _/ | .-~~____--~-/                  ~~==.
     *                       ((->/~   '.|||' -_|    ~~-/ ,              . _||
     *                                  -_     ~\      ~~---l__i__i__i--~~_/
     *                                  _-~-__   ~)  \--______________--~~
     *                                //.-~~~-~_--~- |-------~~~~~~~~
     *                                       //.-~~~--\
     *                       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     *
     *                               神兽保佑            永无BUG
     */
    
    /*
     * @Description: I want to be the weakest vegetable in the world!
     * @Author: I.Z.
     */
    #include<bits/stdc++.h>
    using namespace std;
    #define MOD         998244353
    #define speMOD      2933256077ll
    #define pii         pair<int,int>
    #define all(v)      v.begin(),v.end()
    #define pb          push_back
    #define REP(i,b,e)  for(int i=(b);i<(int)(e);++i)
    #define over(x)     {cout<<(x)<<endl;return;}
    #define lowbit(x)   ((x)&(-(x)))
    #define cntbit(x)   __builtin_popcount(x)
    #define rf(v)       shuffle(all(v),sd);
    #define deal(v)     sort(all(v));v.erase(unique(v.begin(),v.end()),v.end())
    #define lbound(v,x) lower_bound(all(v),x)-v.begin()
    mt19937 sd(random_device{}());
    int qpow(int a,int b,int m=MOD,int res=1){
    	a%=m;
    	while(b>0)res=(b&1)?(res*a%m):(res),a=a*a%m,b>>=1;
    	return res;
    }
    string YN(bool x,string Y="Yes",string N="No") {
    	if(x){
    		return Y;
    	} else {
    		return N;
    	}
    }
    int exgcd(int x,int y,int &a,int &b){
    	if(y==0){
    		a=1;b=0;
    		return x;
    	}
    	int d=exgcd(y,x%y,a,b);
    	int z=b;
    	b=a-b*(x/y);
    	a=z;
    	return d;
    }
    int _x_,_y_;
    inline int inverse(int x,int y=MOD){
    	exgcd(x,y,_x_,_y_);
    	return (_x_+y)%y;
    }
    int fac[2000005],inv[2000005];
    void init(int n){
    	fac[0]=inv[0]=1;
    	REP(i,1,n+1)fac[i]=fac[i-1]*i%MOD;
    	inv[n]=qpow(fac[n],MOD-2);
    	for(int i=n-1;i>=1;--i)inv[i]=inv[i+1]*(i+1)%MOD;
    }
    int binom(int x,int y){
    	return x<y||y<0? 0:fac[x]*inv[y]%MOD*inv[x-y]%MOD;
    }
    struct kosaraju{
    	struct edge {
    	    int v, nxt;
    	} ee[50000000];
    	int n;
    //	vector<int>v[20000000],g[20000000];
    	int h[20000000], hi[20000000];
    	int tot;
    	void addedge(int u, int v) {
    		n=max({n,u,v});
    		ee[++tot].nxt= hi[v];
    		ee[hi[v]=tot].v=u;
    	    ee[++tot].nxt = h[u];
    	    ee[h[u] = tot].v = v;
    	}
    	int bel[20000000];
    	vector<int>t;
    	void dfs(int x){
    		bel[x]=-1;
    		for(int i=h[x];i;i=ee[i].nxt){
    			int v=ee[i].v;
    			if(bel[v]!=-1)dfs(v);
    		}
    		t.pb(x);
    	}
    	void dfs2(int x,int b){
    		bel[x]=b;
    		for(int i=hi[x];i;i=ee[i].nxt){
    			int v=ee[i].v;
    			if(bel[v]==-1)dfs2(v,b);
    		}
    	}
    	bool solve(int m){
    		REP(i,0,n)if(bel[i]!=-1)dfs(i);
    		reverse(all(t));
    		for(auto i:t)if(bel[i]==-1)dfs2(i,i);
    		REP(i,0,m)if(bel[i]==bel[i+m])return 0;
    		return 1;
    	}
    }worker;
    int n,q,L,R;
    vector<int>v[100005],g[100005];
    map<pii,int>wt;
    int tot;
    vector<int>ttt;
    int build1(int l,int r,int p=-1){
    	if(l==r)return ttt[l];
    	if(p==-1)p=tot++;
    	int m=(l+r)>>1;
    	int x=build1(l,m);
    	g[x].pb(p);g[p].pb(x);if(l==m)wt[minmax({x,p})]=1;
    	x=build1(m+1,r);
    	g[x].pb(p);g[p].pb(x);if(m+1==r)wt[minmax({x,p})]=1;
    	return p;
    }
    void dfs(int x,int pre){
    	vector<int>t;
    	for(auto i:v[x])if(i!=pre)dfs(i,x),t.pb(i);
    	if(!t.size())return;
    	if(t.size()<=2){
    		for(auto i:t)g[i].pb(x),g[x].pb(i),wt[minmax({i,x})]=1;
    		return;
    	}
    	ttt=t;build1(0,t.size()-1,x);
    }
    pii mid[200005];
    int ls[20000005],rs[20000005];
    map<pii,int>vis;
    int lts[200005],rts[200005];
    int dep[200005],sz[200005],fa[200005],Rt[200005];
    vector<int>T,te[200005],rt[200005],td[200005];
    int cur;
    map<int,int>d[200005];
    void addedge(int l,int r,int s,int t,int p,int val,int tp){
    	if(l<=s&&t<=r){
    		if(tp&1)worker.addedge(val,p);
    		else worker.addedge(p,val);
    		return;
    	}
    	int m=(s+t)>>1;
    	if(m>=l)addedge(l,r,s,m,ls[p],val,tp);
    	if(m<r)addedge(l,r,m+1,t,rs[p],val,tp);
    }
    void solve(int x,int p,int tp,int val){
    //	cout<<'!'<<x<<' '<<p<<endl;
    	if(lts[p]==-1||rts[p]==-1)return;
    	if(d[lts[p]].find(x)==d[lts[p]].end())swap(lts[p],rts[p]);
    //	cout<<lts[p]<<endl;
    	int r=wt[mid[p]]+d[lts[p]][x];
    //	cout<<lts[p]<<' '<<x<<' '<<r<<endl;
    //	for(auto i:te[p])cout<<i<<' ';
    //	cout<<endl;
    	if(r<=R){
    		int P=rts[p];swap(p,P);
    		int l1=lbound(td[p],L-r),l2=upper_bound(all(td[p]),R-r)-td[p].begin()-1;
    //		cout<<l1<<' '<<l2<<' '<<r<<' '<<"dis("<<Rt[lts[P]]<<','<<x<<")="<<d[Rt[lts[P]]][x]<<endl;
    //		cout<<x+1<<':';
    //		REP(i,l1,l2+1)cout<<te[p][i]+1<<' ';
    //		cout<<endl;
    		if(l1<=l2)addedge(l1,l2,0,te[p].size()-1,rt[p][tp],val,tp),
    		addedge(l1,l2,0,te[p].size()-1,rt[p][tp^3],x*2+n*2-val,tp^3);
    		swap(p,P);
    	}
    	solve(x,lts[p],tp,val);
    }
    vector<int>build2(int l,int r){
    	vector<int>p(4);
    	if(l==r){
    		p[0]=p[1]=T[l];p[2]=p[3]=T[l]+n*2;
    		return p;
    	}
    	REP(i,0,4)p[i]=cur++;
    	int m=(l+r)>>1;
    	vector<int>L=build2(l,m),R=build2(m+1,r);
    	worker.addedge(L[0],p[0]);worker.addedge(p[1],L[1]);
    	worker.addedge(L[2],p[2]);worker.addedge(p[3],L[3]);
    	worker.addedge(R[0],p[0]);worker.addedge(p[1],R[1]);
    	worker.addedge(R[2],p[2]);worker.addedge(p[3],R[3]);
    	REP(i,0,4)ls[p[i]]=L[i],rs[p[i]]=R[i];
    	return p;
    }
    void predfs(int x,int pre){
    	T.pb(x);fa[x]=pre;
    	if(pre!=-1)dep[x]=dep[pre]+wt[minmax({x,pre})];
    	else dep[x]=0;
    	sz[x]=1;
    	for(auto i:g[x])if(i!=pre&&!vis[minmax({i,x})])predfs(i,x),sz[x]+=sz[i];
    }
    int presolve(int t){
    //	cout<<t<<endl;
    	T.clear();predfs(t,-1);
    	int p=tot++;
    	sort(all(T),[&](int x,int y){return dep[x]==dep[y]? x<y:dep[x]<dep[y];});
    	te[p]=T;lts[p]=rts[p]=-1;Rt[p]=t;mid[p]={-1,-1};;
    //	cout<<sz[t]<<endl;
    	rt[p]=build2(0,sz[t]-1);lts[p]=rts[p]=-1;
    //	for(auto i:T)cout<<i<<' '<<dep[i]<<"   ";
    //	cout<<endl;
    	for(auto i:T)d[p][i]=dep[i],td[p].pb(dep[i]);
    	if(sz[t]==1)return p;
    	pii ps={-1,-1};int mn=sz[t];
    	for(auto i:T)if(fa[i]!=-1){
    		int w=llabs(sz[i]*2-sz[t]);
    		if(w<=mn)mn=w,ps=minmax({i,fa[i]});
    	}
    //	cout<<p<<' '<<ps.first<<' '<<ps.second<<endl;
    	if(ps.first>ps.second)swap(ps.first,ps.second);
    	vis[ps]=1;
    	mid[p]=ps;
    	lts[p]=presolve(ps.first);rts[p]=presolve(ps.second);
    	return p;
    }
    void Main() {
    	cin>>n>>q>>L>>R;
    	REP(i,1,n){
    		int x,y;
    		cin>>x>>y;
    		--x,--y;
    		v[x].pb(y);v[y].pb(x);
    	}
    	tot=n;dfs(0,-1);
    //	REP(i,0,tot){
    //		for(auto j:g[i])if(i<j)cout<<i<<' '<<j<<' '<<wt[{i,j}]<<endl;
    //	}
    	tot=0;cur=n*4;presolve(0);
    	// REP(i,0,tot)cout<<i<<' '<<lts[i]<<' '<<rts[i]<<' '<<Rt[i]<<' '<<mid[i].first<<' '<<mid[i].second<<endl;
    	while(q--){
    		int x,y;
    		cin>>x>>y;
    		--x;
    		if(y==0)solve(x,0,0,x);
    		else if(y==1)solve(x,0,2,x);
    		else if(y==2)solve(x,0,0,n*2+x);
    		else solve(x,0,2,n*2+x);
    	}
    	cout<<YN(worker.solve(n*2),"YES","NO")<<endl;
    }
    void TC() {
        int tc=1;
    	while(tc--){
    		Main();
    		cout.flush();
    	}
    }
    signed main() {freopen("misaka.in","r",stdin);freopen("misaka.out","w",stdout);
    	return cin.tie(0),cout.tie(0),ios::sync_with_stdio(0),TC(),0;
    }
    /*
    1. CLEAR the arrays (ESPECIALLY multitests)
    2. DELETE useless output
     */
    
    • 1

    Information

    ID
    2888
    Time
    8000ms
    Memory
    1024MiB
    Difficulty
    10
    Tags
    (None)
    # Submissions
    1
    Accepted
    1
    Uploaded By