用size数组维护数量比较好理解
在C合并的时候数量相加也比较好理解
但是需要注意,两个相同集合合并时,数量不能相加
MAX_LENGTH = 100010
p = [i for i in range(MAX_LENGTH)]
c = [1] * MAX_LENGTH
def find(x):
t, f = x, p[x]
while f != t:
t = f
f = p[t]
i, f = x, p[x]
while f != i:
p[i] = t
i = f
f = p[i]
return t
if __name__ == '__main__':
n, m = map(int, input().split())
for _ in range(m):
s = input().split()
if s[0] == 'C':
i, j = int(s[1]), int(s[2])
if find(j) != find(i):
c[find(j)] += c[find(i)]
p[find(i)] = find(j)
elif s[0] == 'Q1':
i, j = int(s[1]), int(s[2])
print('Yes' if find(i) == find(j) else 'No')
elif s[0] == 'Q2':
i = int(s[1])
print(c[find(i)])