考虑给每个点 赋一个随机权值 ,设 。
由于所有点都能到达 的限制等价于除了 以外的所有点都有恰好一条出边,设点 有 条出边,那么根据异或哈希的性质可知,当 时满足题目中给出的条件。
于是时刻维护 的值即可。
mt19937_64 rng(random_device{}());
constexpr int N = 1e6+5;
int n,k,q;
vector<pair<int,bool>> G[N];
int f[N];
ull h[N],sah[N],ssh[N],nsh[N],sh;
bool typ[N];
unordered_set<int> se[N];
void dfs(int u,int fa){
f[u]=fa;
for(auto [v,w]:G[u]){
if(v==fa)continue;
h[v]=rng();
sah[v]=sah[u]^h[v];
ssh[u]^=h[v];
if(w){
se[u].insert(v);
nsh[u]^=h[v];
sh^=h[v];
}
dfs(v,u);
}
}
void out(){
if(sh==sah[k])cout<<"YES\n";
else cout<<"NO\n";
}
bool get(int u){
return se[f[u]].count(u)^typ[f[u]];
}
void rev(int u){
nsh[f[u]]^=h[u];
sh^=h[u];
if(se[f[u]].count(u))se[f[u]].erase(u);
else se[f[u]].insert(u);
}
void main(){
cin>>n>>k>>q;
rep(i,2,n){
int u,v;
cin>>u>>v;
G[u].emplace_back(v,1);
G[v].emplace_back(u,0);
}
dfs(1,0);
out();
while(q--){
int op,u,v;
cin>>op>>u;
if(op==1){
cin>>v;
if(f[v]!=u)swap(u,v);
rev(v);
}
if(op==2){
sh^=nsh[u]^ssh[u];
nsh[u]=ssh[u];
typ[u]=1;
se[u].clear();
if(u>1&&get(u))rev(u);
}
if(op==3){
sh^=nsh[u];
nsh[u]=0;
typ[u]=0;
se[u].clear();
if(u>1&&!get(u))rev(u);
}
if(op==4){
sh^=ssh[u];
nsh[u]^=ssh[u];
typ[u]^=1;
if(u>1)rev(u);
}
if(op==5){
k=u;
}
out();
}
}