| 记录编号 |
397271 |
评测结果 |
AAAAA |
| 题目名称 |
2281.[HZOI 2015]白黑树 |
最终得分 |
100 |
| 用户昵称 |
AntiLeaf |
是否通过 |
通过 |
| 代码语言 |
C++ |
运行时间 |
0.261 s |
| 提交时间 |
2017-04-19 21:38:40 |
内存使用 |
7.37 MiB |
显示代码纯文本
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
const int maxn=200010;
void dfs1(int);
void dfs2(int);
int LCA(int,int);
template<typename cmp>struct binary_heap{
priority_queue<int,vector<int>,cmp>q1,q2;
void push(int x){q1.push(x);}
void erase(int x){q2.push(x);}
int top(){
while(!q2.empty()&&q1.top()==q2.top()){
q1.pop();
q2.pop();
}
return q1.top();
}
bool empty(){
while(!q2.empty()&&q1.top()==q2.top()){
q1.pop();
q2.pop();
}
return q1.empty();
}
};
vector<int>G[maxn];
int p[maxn],d[maxn],dfn[maxn],tim=0;
int size[maxn],son[maxn],top[maxn];
struct cmp1{cmp1(){}bool operator()(int x,int y){return dfn[x]<dfn[y];}};
binary_heap<cmp1>heap1;
struct cmp2{cmp2(){}bool operator()(int x,int y){return dfn[x]>dfn[y];}};
binary_heap<cmp2>heap2;
bool col[maxn]={false};
char c;
int n,m,x,y;
int main(){
freopen("C_Tree.in","r",stdin);
freopen("C_Tree.out","w",stdout);
scanf("%d%d",&n,&m);
for(int i=1;i<n;i++){
scanf("%d%d",&x,&y);
G[x].push_back(y);
G[y].push_back(x);
}
dfs1(1);
dfs2(1);
while(m--){
scanf(" %c%d",&c,&x);
if(c=='M'){
if(col[x]){
heap1.erase(x);
heap2.erase(x);
}
else{
heap1.push(x);
heap2.push(x);
}
col[x]^=true;
}
else{
if(heap1.empty())printf("-1\n");
else{
int u=LCA(x,heap1.top()),v=LCA(x,heap2.top());
printf("%d\n",d[u]<d[v]?u:v);
}
}
}
return 0;
}
void dfs1(int x){
dfn[x]=++tim;
size[x]=1;
d[x]=d[p[x]]+1;
for(int i=0;i<(int)G[x].size();i++)if(G[x][i]!=p[x]){
p[G[x][i]]=x;
dfs1(G[x][i]);
size[x]+=size[G[x][i]];
if(size[G[x][i]]>size[son[x]])son[x]=G[x][i];
}
}
void dfs2(int x){
if(x==son[p[x]])top[x]=top[p[x]];
else top[x]=x;
for(int i=0;i<(int)G[x].size();i++)if(G[x][i]!=p[x])dfs2(G[x][i]);
}
int LCA(int x,int y){
while(top[x]!=top[y]){
if(d[top[x]]<d[top[y]])swap(x,y);
x=p[top[x]];
}
return d[x]<d[y]?x:y;
}