bzoj 2049
lct裸题
(现在饿的不敢再想吃的了
)
2015.9.11:
这道题的关键就是“任意时刻任意两点之间最多有一条路”。
#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
#define N 10010
struct node{
node *fa;
node *ch[2];
int rev;
int root;
int val;
void init(int tempval){
fa=NULL;
ch[0]=NULL;
ch[1]=NULL;
rev=0;
root=tempval;
val=tempval;
}
bool isroot(){
return fa==NULL||(fa->ch[0]!=this&&fa->ch[1]!=this);
}
void fswitch(){
rev^=1;
swap(ch[0],ch[1]);
}
void push_down(){
if(rev){
if(ch[0]){
ch[0]->fswitch();
}
if(ch[1]){
ch[1]->fswitch();
}
rev=0;
}
return;
}
void go(){
if(!(isroot())){
fa->go();
}
push_down();
return;
}
int dir(){
return fa->ch[1]==this?1:0;
}
void setedge(int d,node *another){
ch[d]=another;
if(another){
another->fa=this;
}
}
void push_up(){
if(ch[0]){
root=ch[0]->root;
}
else{
root=val;
}
return;
}
void rot(){
int d=dir();
node *tempfafa=fa->fa;
if(!(fa->isroot())){
tempfafa->ch[fa->dir()]=this;
}
fa->setedge(d,ch[!d]);
setedge(!d,fa);
fa=tempfafa;
ch[!d]->push_up();
return;
}
void splay(){
go();
while(!isroot()){
if(!(fa->isroot())){
dir()==fa->dir()?fa->rot():rot();
}
rot();
}
push_up();
return;
}
void access(){
for(node *p=this,*q=NULL;p!=NULL;q=p,p=p->fa){
p->splay();
p->setedge(1,q);
p->push_up();//针对于这道题来说,虽然可以不用,但是不要破坏队形
}
splay();
return;
}
void make_root(){
access();
fswitch();
return;
}
void cut(node *another){
make_root();
another->access();
another->ch[0]->fa=NULL;
another->ch[0]=NULL;
another->push_up();
return;
}
void link(node *another){
another->make_root();//这里是make_root而不是access
another->fa=this;
}
int query(node *another){
make_root();
another->access();
if(another->root==this->val){
return true;
}
else{
return false;
}
}
};
node *tree[N],pool[N];
int main(){
int n,m;
char op[10];
int a,b;
while(scanf("%d%d",&n,&m)!=EOF){
for(int i=1;i<=n;i++){
tree[i]=&(pool[i]);
tree[i]->init(i);
}
for(int i=0;i<m;i++){
scanf("%s",op);
scanf("%d%d",&a,&b);
if(op[0]=='C'){
tree[a]->link(tree[b]);
}
else if(op[0]=='D'){
tree[a]->cut(tree[b]);
}
else{
if(tree[a]->query(tree[b])){
printf("Yes\n");
}
else{
printf("No\n");
}
}
}
}
return 0;
}

浙公网安备 33010602011771号