use std::io::{read_to_string, stdin}; struct ReligionState { parents: Vec, counts: Vec, } impl ReligionState { fn find(&self, x: usize) -> usize { let mut node = x; while (node != self.parents[node]) { node = self.parents[node]; } return node; } fn sames(&self, x: usize) -> usize { return self.counts[self.find(x)]; } fn unite(&mut self, x: usize, y: usize) { let rx = self.find(x); let ry = self.find(y); if rx < ry { self.parents[ry] = rx; self.counts[rx] += self.counts[ry]; } else if rx > ry { self.parents[rx] = ry; self.counts[ry] += self.counts[rx] } else { } } } fn main() { let temp = read_to_string(stdin()).unwrap(); let mut iter = temp .split_ascii_whitespace() .map(|x| x.parse::().unwrap()); let n = iter.next().unwrap(); let q = iter.next().unwrap(); let mut parents = (0..n).map(|i| i).collect::>(); let mut S = ReligionState { parents, counts: (0..n).map(|_| 1).collect(), }; for _ in 0..q { let query = iter.next().unwrap(); if (query == 1) { let (x, y) = (iter.next().unwrap() - 1, iter.next().unwrap() - 1); S.unite(x, y); } else { let x = iter.next().unwrap() - 1; println!("{}", S.sames(x)); } } }