## https://yukicoder.me/problems/no/556 class UnionFind: """ UnionFindの基本的な処理を実装したクラス """ def __init__(self, size): self.root = [i for i in range(size)] self.size = [1] * size def get_root(self, v): if v == self.root[v]: return v else: old_root = self.root[v] new_root = self.get_root(old_root) self.root[v] = new_root return new_root def merge(self, u, v): root_u = self.get_root(u) root_v = self.get_root(v) if root_u == root_v: return False if self.size[root_u] > self.size[root_v]: self.size[root_u] += self.size[root_v] self.root[root_v] = root_u self.root[v] = root_u elif self.size[root_u] < self.size[root_v]: self.size[root_v] += self.size[root_u] self.root[root_u] = root_v self.root[u] = root_v else: if root_u < root_v: self.size[root_u] += self.size[root_v] self.root[root_v] = root_u self.root[v] = root_u else: self.size[root_v] += self.size[root_u] self.root[root_u] = root_v self.root[u] = root_v return True def main(): N, M = map(int, input().split()) ab = [] for _ in range(M): a, b = map(int, input().split()) ab.append((a - 1 ,b - 1)) uf = UnionFind(N) for a, b in ab: uf.merge(a, b) for i in range(N): a = uf. get_root(i) print(a + 1) if __name__ == "__main__": main()