class UF(): def __init__(self, N): self._parent=[n for n in range(0, N)] self._size=[1] * N def find_root(self, x): if self._parent[x]==x:return x self._parent[x]=self.find_root(self._parent[x]) return self._parent[x] def unite(self, x, y): gx=self.find_root(x) gy=self.find_root(y) if gx==gy:return if self._size[gx]Y: rx=UFa.find_root(x-1) ry=UFa.find_root(y-1) for j in member[ry]: ans[j]=0 UFa.unite(x-1,y-1) r=UFa.find_root(x-1) if r==rx: for j in member[ry]: member[rx].add(j) member[ry]=set() else: for j in member[rx]: member[ry].add(j) member[rx]=set() else: rx=UFa.find_root(x-1) ry=UFa.find_root(y-1) for j in member[rx]: ans[j]=0 UFa.unite(x-1,y-1) r=UFa.find_root(x-1) if r==rx: for j in member[ry]: member[rx].add(j) member[ry]=set() else: for j in member[rx]: member[ry].add(j) member[rx]=set() for i in range(n): print(ans[i])