analysis
分析可知,用树剖
关键在于将边权转化为点权
我们可以将边权向下压,压到两边的点中深度较大的的那个点,然后通过求路径上的点权值和是否为0来判断是否可通过(注意要权值和要减去LCA的权值,因为路径并不会经过LCA和它上面那个点之间的连边)
真的真的不要把线段树和树剖打爆了啊啊啊啊啊啊啊啊啊
code
#include<bits/stdc++.h>
using namespace std;
#define loop(i,start,end) for(register int i=start;i<=end;++i)
#define anti_loop(i,start,end) for(register int i=start;i>=end;--i)
#define clean(arry,num) memset(arry,num,sizeof(arry))
#define max(a,b) ((a>b)?a:b)
#define min(a,b) ((a<b)?a:b)
#define ll long long
const int maxn=300000+10,maxm=300000+10;
int n,m,cnt=0;
template<typename T>void read(T &x){
x=0;char r=getchar();T neg=1;
while(r>'9'||r<'0'){if(r=='-')neg=-1;r=getchar();}
while(r>='0'&&r<='9'){x=(x<<1)+(x<<3)+r-'0';r=getchar();}
x*=neg;
}
int dep[maxn],son[maxn],fa[maxn],siz[maxn];
int seg[maxn],rev[maxn],top[maxn];
struct node{
int e;int nxt;
}edge[maxn<<1];
int head[maxn];
int sum[maxn<<2],lazy[maxn<<2];
struct oppo{int a;int b;};
int warx[maxn],wary[maxn];
inline void addl(int u,int v){
edge[cnt].e=v;
edge[cnt].nxt=head[u];
head[u]=cnt++;
}
void dfs1(int u,int f){
dep[u]=dep[f]+1;
siz[u]=1;fa[u]=f;
son[u]=0;
for(int i=head[u];i!=-1;i=edge[i].nxt){
int v=edge[i].e;
if(v==f)continue;
dfs1(v,u);
siz[u]+=siz[v];
if(siz[v]>siz[son[u]])son[u]=v;
}
}
void dfs2(int u,int f){
if(son[u]){
seg[son[u]]=++seg[0];
rev[seg[0]]=son[u];
top[son[u]]=top[u];
dfs2(son[u],u);
}
for(int i=head[u];i!=-1;i=edge[i].nxt){
int v=edge[i].e;
if(!top[v]){
seg[v]=++seg[0];
rev[seg[0]]=v;
top[v]=v;
dfs2(v,u);
}
}
}
inline void buildtree(){clean(sum,0);}
void update(int p,int nl,int nr,int rt,int w){
if(nl==nr&&nr==p){
sum[rt]+=w;
return;
}
int mid=((nr+nl)>>1);
if(mid>=p)update(p,nl,mid,rt<<1,w);
else update(p,mid+1,nr,rt<<1|1,w);
sum[rt]=sum[rt<<1]+sum[rt<<1|1];
}
int query(int l,int r,int nl,int nr,int rt){
if(l<=nl&&nr<=r){
return sum[rt];
}
int mid=((nr+nl)>>1);
int _sum=0;
if(mid>=l)_sum+=query(l,r,nl,mid,rt<<1);
if(mid<r)_sum+=query(l,r,mid+1,nr,rt<<1|1);
return _sum;
}
inline void reply(){
int numofWar=0;
loop(i,1,m){
register char op[100];
scanf("%s",op);
if(op[0]=='Q'){
int x,y;read(x),read(y);
int fx=top[x],fy=top[y];
//bool access=true;
int _sum=0;
while(fx!=fy){
if(dep[fx]<dep[fy])swap(fx,fy),swap(x,y);
_sum+=query(seg[fx],seg[x],1,seg[0],1);
x=fa[fx],fx=top[x];
}
if(dep[x]<dep[y])swap(x,y);
_sum+=query(seg[y],seg[x],1,seg[0],1);
_sum-=query(seg[y],seg[y],1,seg[0],1);
if(_sum>0)printf("No\n");
else printf("Yes\n");
}
else if(op[0]=='C'){
int x,y;read(x),read(y);
warx[++numofWar]=x;
wary[numofWar]=y;
update(seg[((dep[x]<dep[y])?y:x)],1,seg[0],1,1);//update(((dep[x]<dep[y])?y:x),1,seg[0],1,1);
}
else if(op[0]=='U'){
int x;read(x);
update(seg[((dep[wary[x]]>dep[warx[x]])?wary[x]:warx[x])],1,seg[0],1,-1);//update(((dep[wary[x]]>dep[warx[x]])?wary[x]:warx[x]),1,seg[0],1,-1);
}
}
}
int main(){
#ifndef ONLINE_JUDGE
freopen("datain2.txt","r",stdin);
#endif
clean(dep,0),clean(head,-1),clean(son,0),clean(fa,0),clean(siz,0),clean(seg,0),clean(rev,0),clean(top,0);
read(n),read(m);
loop(i,1,n-1){
int xi,yi;read(xi),read(yi);
addl(xi,yi),addl(yi,xi);
}
dfs1(1,0);
seg[1]=seg[0]=rev[1]=top[1]=1;
dfs2(1,0);
//loop(i,1,n)printf("point:%d dep:%d top:%d son:%d siz:%d seg:%d rev:%d fa:%d\n",i,dep[i],top[i],son[i],siz[i],seg[i],rev[i],fa[i]);
reply();
return 0;
}