def root(uf, i) if uf[i] < 0 i else root(uf, uf[i]) end end def count(uf, i) r = root(uf, i) -uf[r] end def merge(uf, i, j) u = root(uf, i) v = root(uf, j) return if u == v a = count(uf, u) b = count(uf, v) if a > b || (a == b && u < v) uf[v] = u uf[j] = u uf[u] = -(a + b) else uf[u] = v uf[i] = v uf[v] = -(a + b) end end n, m = gets.split.map(&:to_i) uf = [-1] * n m.times do a, b = gets.split.map(&:to_i) merge(uf, a - 1, b - 1) end n.times do |i| p (root(uf, i) + 1) end