Description
Solution
直接LCT维护即可。。
Code
/*****************
Au: Hany01
Date: Dec 31th, 2017
Prob: bzoj2049 & sdoi2008 cave
Email: hany01@foxmail.com
*****************/
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
typedef vector<int> VI;
#define rep(i , j) for (int i = 0 , i##_end_ = j; i < i##_end_ ; ++ i)
#define For(i , j , k) for (int i = (j) , i##_end_ = (k) ; i <= i##_end_ ; ++ i)
#define Fordown(i , j , k) for (int i = (j) , i##_end_ = (k) ; i >= i##_end_ ; -- i)
#define Set(a , b) memset(a , b , sizeof(a))
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
#define INF (0x3f3f3f3f)
#define INF1 (2139062143)
#define Mod (1000000007)
#ifdef hany01
#define debug(...) fprintf(stderr , __VA_ARGS__)
#else
#define debug(...)
#endif
inline void file()
{
#ifdef hany01
freopen("bzoj2049.in" , "r" , stdin);
freopen("bzoj2049.out" , "w" , stdout);
#endif
}
template<typename T> inline bool chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; }
template<typename T> inline bool chkmin(T &a, T b) { return b < a ? a = b, 1 : 0; }
char c_; int _ , __;
inline int read()
{
for (_ = 0 , __ = 1 , c_ = getchar() ; !isdigit(c_) ; c_ = getchar()) if (c_ == '-') __ = -1;
for ( ; isdigit(c_) ; c_ = getchar()) _ = (_ << 1) + (_ << 3) + (c_ ^ 48);
return _ * __;
}
const int maxn = 300005;
#define dir(x) (ch[fa[x]][0] != (x))
#define isrt(x) (ch[fa[x]][0] != x && ch[fa[x]][1] != x)
struct Link_Cut_Trees
{
int fa[maxn], ch[maxn][2], rev[maxn];
inline void pushdown(int o)
{
if (rev[o]) {
rev[ch[o][0]] ^= 1; rev[ch[o][1]] ^= 1;
swap(ch[o][0], ch[o][1]); rev[o] = 0;
}
}
inline void rotate(int o)
{
register int f = fa[o], gf = fa[f], d = dir(o);
fa[ch[f][d] = ch[o][d ^ 1]] = f;
fa[o] = gf;
if (!isrt(f)) ch[gf][dir(f)] = o;
ch[fa[f] = o][d ^ 1] = f;
}
int top, stk[maxn];
inline void splay(int o)
{
stk[top = 1] = o;
for (register int t = o; !isrt(t); t = fa[t]) stk[++ top] = fa[t];//fa[t]不能写成了t
while (top) pushdown(stk[top --]);
for ( ; !isrt(o); rotate(o)) if (!isrt(fa[o])) rotate(dir(o) == dir(fa[o]) ? fa[o] : o);
}
inline void access(int o)
{
for (register int t = 0; o; t = o, o = fa[o]) splay(o), ch[o][1] = t;
}
inline void makeroot(int o) { access(o), splay(o), rev[o] ^= 1; }
inline int findroot(int o)
{
access(o), splay(o);
while (ch[o][0]) o = ch[o][0];
return o;
}
inline void link(int x, int y)
{
if (findroot(x) == findroot(y)) return ;
makeroot(x), fa[x] = y;
}
inline void cut(int x, int y)
{
makeroot(x), access(y), splay(y);
if (ch[y][0] == x) ch[y][0] = 0, fa[x] = 0;
}
inline void isconnected(int x, int y)
{
if (findroot(x) == findroot(y)) puts("Yes"); else puts("No");
}
}lct;
int main()
{
file();
register int n = read(), m = read(), x, y;
register char op[10];
while (m --) {
scanf("%s", op); x = read(); y = read();
if (*op == 'C') lct.link(x, y);
else if (*op == 'D') lct.cut(x, y);
else lct.isconnected(x, y);
}
return 0;
}
//渔舟逐水爱山春,两岸桃花夹古津。
// -- 王维《桃源行》