class UnionFind def initialize(n) @size = Array.new(n, 1) @b = Array.new(n, 0) @parent = [] (0..n).each do |i| @parent[i] = i end end def find(x) if @parent[x] == x x else find(@parent[x]) end end def add(x, v) root = find(x) @b[root] += v end def get(x) if @parent[x] == x @b[x] else @b[x] + get(@parent[x]) end end def unite(x, y) x = find(x) y = find(y) return if x == y if @size[x] < @size[y] @parent[x] = y @size[y] += @size[x] @b[x] -= @b[y] else @parent[y] = x @size[x] += @size[y] @b[y] -= @b[x] end end def same?(x, y) find(x) == find(y) end def size(x) @size[find(x)] end end N, Q = gets.split.map(&:to_i) uf = UnionFind.new(N + 1) A = Hash.new(0) Q.times do t, a, b = gets.split.map(&:to_i) case t when 1 uf.unite(a, b) when 2 uf.add(a, b) when 3 puts uf.get(a) end end