题目链接
Description
上决╇ф发现一个很有趣的事情,现在很多人都有他们的大哥。毕竟嘛,大哥可以罩着他们,他们就能为所欲为了。但是,总有大哥不靠谱的时候,于是乎,他们就会选择换一个大哥。
现在问题来了,当两个人发生争执的时候,如果他们有共同的大哥,他们就会和解。否则他们就会打起来。请注意,每个人只有一个直接的大哥,并且大哥的大哥,也是大哥。
Input
第一行,包含两个正整数nn,mm。分别代表会有nn个人和mm个事件将会发生。起初,这nn个人都没有大哥。
接下来mm行,分别代表按时间顺序发生的时间,只可能存在两种事件:
1、call x y。编号为xx的人,将自己的直接大哥修改为编号为yy的人。
2、query x y。编号为xx的人和编号为y的人发生了矛盾,现在需要查询他们是否有共同的大哥。
输入数据保证大哥关系不会构成环。
1≤n,m≤1051≤n,m≤105,1≤x,y≤n1≤x,y≤n
Output
对于第二种事件,如果他们有共同的大哥,达成了和解,则输出“Yes”,否则输出“No”。
题目看了就能发现是一道动态树的问题,当然,可持久化并查集也是可以的(蒻,我不会写~)。
这道题让我知道了一件事,(也就是我疯狂打注释的那块)在删去边的时候,就算知道合法性也要把两个点放到一起去,因为他们一定直接相连,所以深度差一定为1,故提取出来之后再直接删除即可。
后面带了几组测试样例。
#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#define lowbit(x) ( x&(-x) )
#define pi 3.141592653589793
#define e 2.718281828459045
#define INF 0x3f3f3f3f
#define HalF (l + r)>>1
#define lsn rt<<1
#define rsn rt<<1|1
#define Lson lsn, l, mid
#define Rson rsn, mid+1, r
#define QL Lson, ql, qr
#define QR Rson, ql, qr
#define myself rt, l, r
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int maxN = 1e5 + 7;
int N, M, c[maxN][2], r[maxN], fa[maxN], st[maxN], root[maxN];
bool isroot(int x) { return c[fa[x]][0] != x && c[fa[x]][1] != x; }
inline void pushr(int x) { swap(c[x][0], c[x][1]); r[x] ^= 1; }
inline void pushdown(int x)
{
if(r[x])
{
if(c[x][0]) pushr(c[x][0]);
if(c[x][1]) pushr(c[x][1]);
r[x] = 0;
}
}
inline void Rotate(int x)
{
int y = fa[x], z = fa[y], k = c[y][1] == x;
if(!isroot(y)) c[z][c[z][1] == y] = x;
fa[x] = z;
c[y][k] = c[x][k^1];
fa[c[x][k^1]] = y;
c[x][k^1] = y;
fa[y] = x;
}
inline void splay(int x)
{
int y = x, z = 0;
st[++z] = y;
while(!isroot(y)) st[++z] = y = fa[y];
while(z) pushdown(st[z--]);
while(!isroot(x))
{
y = fa[x]; z = fa[y];
if(!isroot(y)) (c[z][0] == y) ^ (c[y][0] == x) ? Rotate(x) : Rotate(y);
Rotate(x);
}
}
inline void access(int x)
{
int y = 0;
while(x)
{
splay(x); c[x][1] = y;
y = x; x = fa[x];
}
}
inline void makeroot(int x)
{
access(x); splay(x);
pushr(x);
}
inline int findroot(int x)
{
access(x); splay(x);
while(c[x][0]) { pushdown(c[x][0]); x = c[x][0]; }
splay(x);
return x;
}
inline void link(int x, int y)
{
makeroot(x);
if(findroot(y) != x) fa[x] = y;
}
inline void cut(int x, int y) //因为x与y是直接相连的
{
makeroot(x); access(y); splay(x); //让x、y直接相连,因为x、y的深度差一定是1
//if(findroot(y) != x) return; //虽然一定是有边,但是我们依然要求这个判断,并且不需要其他判断,因为保证了他们一定直接相连
fa[y] = c[x][1] = 0;
}
bool check(int x, int y)
{
makeroot(x);
return findroot(y) == x;
}
char op[10];
int main()
{
scanf("%d%d", &N, &M);
int x, y;
while(M--)
{
scanf("%s%d%d", op, &x, &y);
if(op[0] == 'c')
{
if(root[x]) cut(root[x], x);
link(x, y);
root[x] = y;
}
else printf(check(x, y) ? "Yes\n" : "No\n");
}
return 0;
}
/*
10 20
query 10 2
call 2 10
call 4 2
query 9 4
query 10 4
call 9 2
query 8 5
call 9 10
query 10 4
query 10 7
query 3 7
query 2 10
query 8 1
query 10 4
call 7 3
call 6 8
call 7 8
call 3 9
query 3 7
query 6 10
*/
/*
10 15
call 2 10
call 4 2
call 9 2
call 9 10
call 7 3
call 6 8
call 7 8
call 3 9
call 10 8
call 10 5
call 7 2
call 7 6
call 10 7
call 2 3
query 6 5
*/