BZOJ 2049: [Sdoi2008]Cave 洞穴勘测( LCT )

LCT....

---------------------------------------------------------------------------------

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>

#define rep( i , n ) for( int i = 0 ; i < n ; ++i )
#define clr( x , c ) memset( x , c , sizeof( x ) )

using namespace std;

const int maxn = 10000 + 5;

struct Node *pt , *null;

struct Node {
Node *ch[ 2 ] , *p , *fa;
bool isRoot , rev;
Node() {
rev = false;
isRoot = true;
ch[ 0 ] = ch[ 1 ] = fa = p = null;
}
inline void setc( Node* c , int d ) {
ch[ d ] = c;
c -> p = this;
}
inline void relax() {
if( rev ) {
swap( ch[ 0 ] , ch[ 1 ] );
rep( i , 2 ) if( ch[ i ] != null )
ch[ i ] -> Rev();
rev = false;
}
}
inline void Rev() {
rev ^= 1;
}
inline bool d() {
return this == p -> ch[ 1 ];
}
inline void setRoot( Node* f ) {
fa = f;
isRoot = true;
p = null;
}
void* operator new( size_t ) {
return pt++;
}
};

Node NODE[ maxn ];

void rot( Node* t ) {
Node* p = t -> p;
p -> relax();
t -> relax();
bool d = t -> d();
p -> p -> setc( t , p -> d() );
p -> setc( t -> ch[ ! d ] , d );
t -> setc( p , ! d );
if( p -> isRoot ) {
p -> isRoot = false;
t -> isRoot = true;
t -> fa = p -> fa;
}
}

void splay( Node* t , Node* f = null ) {
static Node* S[ maxn ];
int top = 0;
for( Node* o = t ; o != null ; o = o -> p )
S[ ++top ] = o;
for( ; top ; top-- )
S[ top ] -> relax();
for( Node* p = t -> p ; p != f ; p = t -> p ) {
if( p -> p == f ) rot( t );
else if( t -> d() != p -> d()) rot( t ) , rot( t );
else rot( p ) , rot( t );
}
}

void access( Node* t ) {
for( Node* o = null ; t != null ; o = t , t = t -> fa ) {
splay( t );
t -> ch[ 1 ] -> setRoot( t );
t -> setc( o , 1 );
}
}

Node* findRoot( Node* t ) {
access( t );
splay( t );
while( t -> ch[ 0 ] != null ) {
t -> relax();
t = t -> ch[ 0 ];
}
splay( t );
return t;
}

void makeRoot( Node* t ) {
access( t );
splay( t );
t -> Rev();
}

void cut( Node* u , Node* v ) {
makeRoot( u );
access( v );
splay( u );
u -> setc( null , 1 );
v -> isRoot = true;
v -> p = v -> fa = null;
}

void join( Node* u , Node* v ) {
makeRoot( v );
v -> fa = u;
}

void init() {
pt = NODE;
null = new( Node );
}

Node* V[ maxn ];

int main() {
freopen( "test.in" , "r" , stdin );
init();
int n , m;
cin >> n >> m;
rep( i , n ) V[ i ] = new( Node );
char s[ 10 ];
int u , v;
while( m-- ) {
scanf( "%s%d%d" , s , &u , &v );
u-- , v--;
if( s[ 0 ] == 'Q' )
printf( findRoot( V[ u ] ) != findRoot( V[ v ] ) ? "No\n" : "Yes\n" );
else if( s [ 0 ] == 'C' )
join( V[ u ] , V[ v ] );
else
cut( V[ u ] , V[ v ] );
}
return 0;
}

---------------------------------------------------------------------------------

2049: [Sdoi2008]Cave 洞穴勘测

Time Limit: 10 Sec  Memory Limit: 259 MB
Submit: 4319  Solved: 1945
[Submit][Status][Discuss]

200 5
Query 123 127
Connect 123 127
Query 123 127
Destroy 127 123
Query 123 127

3 5
Connect 1 2
Connect 3 1
Query 2 3
Destroy 1 3
Query 2 3

No
Yes
No

Yes
No

Source

posted @ 2015-06-17 21:58  JSZX11556  阅读(...)  评论(... 编辑 收藏