社区讨论
玄关求条,Only AC test2
P3950部落冲突参与者 2已保存回复 9
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 9 条
- 当前快照
- 1 份
- 快照标识符
- @mhjib4fo
- 此快照首次捕获于
- 2025/11/04 03:01 4 个月前
- 此快照最后确认于
- 2025/11/04 03:01 4 个月前
不知道为什么仅 AC test2,应该是什么细节没处理到。
CPP#include<bits/stdc++.h>
using namespace std;
const int N=3e5+5;
struct STree{
struct node{
int l,r;
bool dat;
}t[N<<2];
inline void push_up(int p){
t[p].dat=(t[p<<1].dat||t[p<<1|1].dat);
}
inline void build(int p,int l,int r){
t[p].l=l;t[p].r=r;t[p].dat=false;
if (l==r) return ;
int mid=(l+r)>>1;
build(p<<1,l,mid);
build(p<<1|1,mid+1,r);
}
inline void change(int p,int x){
if (t[p].l==t[p].r){
t[p].dat=!t[p].dat;
return ;
}int mid=(t[p].l+t[p].r)>>1;
if (x<=mid) change(p<<1,x);
else change(p<<1|1,x);
push_up(p);
}
inline bool ask(int p,int l,int r){
if (l<=t[p].l&&t[p].r<=r)
return t[p].dat;
int mid=(t[p].l+t[p].r)>>1;
bool val=false;
if (l<=mid) val|=ask(p<<1,l,r);
if (r>mid) val|=ask(p<<1|1,l,r);
return val;
}
}st;
int n,q,p[N],tot;
int dep[N],f[N],son[N],sz[N];
int dfn[N],rnk[N],top[N],cnt;
vector<int> edge[N];
inline void dfs1(int x){
sz[x]=1;
son[x]=-1;
for (auto y:edge[x])
if (!dep[y]){
dep[y]=dep[x]+1;
f[y]=x;
dfs1(y);
sz[x]+=sz[y];
if (son[x]==-1||sz[y]>sz[son[x]]) son[x]=y;
}
}
inline void dfs2(int x,int t){
top[x]=t;
dfn[x]=++cnt;
rnk[cnt]=x;
if (son[x]==-1) return ;
dfs2(son[x],t);
for (auto y:edge[x])
if (y!=f[x]&&y!=son[x])
dfs2(y,y);
}
inline bool get_ans(int x,int y){
bool ans=false;
while (top[x]!=top[y]){
if (dep[top[x]]<dep[top[y]]) swap(x,y);
ans|=st.ask(1,dfn[top[x]],dfn[x]);
if (ans) return true;
x=f[top[x]];
}if (dfn[x]>dfn[y]) swap(x,y);
return ans|st.ask(1,dfn[x]+1,dfn[y]);
}
int main(){
cin>>n>>q;
for (int i=1,x,y;i<n;i++){
cin>>x>>y;
edge[x].push_back(y);
edge[y].push_back(x);
}dep[1]=1;dfs1(1);dfs2(1,1);
st.build(1,1,n);
while (q--){
char opt;int x,y;
cin>>opt>>x;
if (opt!='U') cin>>y;
if (dep[x]>dep[y]) swap(x,y);
if (opt=='Q') puts(get_ans(x,y)?"No":"Yes");
else if (opt=='C') st.change(1,dfn[y]),p[++tot]=dfn[y];
else st.change(1,p[x]);
}
return 0;
}
回复
共 9 条回复,欢迎继续交流。
正在加载回复...