AcWing 837. 连通块中点的数量 python
原题链接
简单
作者:
申侠
,
2020-10-23 18:42:49
,
所有人可见
,
阅读 296
class USD(object):
def __init__(self, n):
self.lt = [i for i in range(n+1)]
self.cnt = [1 for i in range(n+1)]
def find(self, a):
if self.lt[a] != a:
self.lt[a] = self.find(self.lt[a])
return self.lt[a]
def merge(self,a,b):
self.cnt[self.find(b)] += self.cnt[self.find(a)]
self.lt[self.find(a)] = self.find(b)
def query(self,a,b):
return 'Yes' if self.find(a) == self.find(b) else 'No'
def get_cnt(self,a):
return self.cnt[self.find(a)]
n,m = map(int, input().split(' '))
usd = USD(n)
for _ in range(m):
cmd= list(map(str, input().split(' ')))
if cmd[0] == 'C':
usd.merge(int(cmd[1]),int(cmd[2]))
elif cmd[0] == 'Q1':
print(usd.query(int(cmd[1]),int(cmd[2])))
else:
print(usd.get_cnt(int(cmd[1])))