AcWing 837. 连通块中点的数量python3
原题链接
简单
作者:
xanxus1111
,
2020-05-06 22:28:00
,
所有人可见
,
阅读 601
def find(x):
if p[x]!=x: p[x] = find(p[x])
return p[x]
if __name__ == '__main__':
n,m = map(int,input().split())
size = [1]*(n+1)
p = [i for i in range(0,n+1)]
for i in range(m):
s = input()
if s[0:2] == 'Q1':
a,b = map(int, s[2:].split())
if find(a) == find(b): print('Yes')
else: print('No')
elif s[0:2] == 'Q2':
a = int(s[2:])
print(size[find(a)])
else:
a, b = map(int, s[2:].split())
if find(a) == find(b):continue
size[find(b)]+=size[find(a)]
p[find(a)] = find(b)