#[allow(unused_imports)] use std::cmp::{max, min}; #[allow(unused_imports)] use std::collections::{HashMap, HashSet}; mod util { use std::io::stdin; use std::str::FromStr; use std::fmt::Debug; #[allow(dead_code)] pub fn line() -> String { let mut line: String = String::new(); stdin().read_line(&mut line).unwrap(); line.trim().to_string() } #[allow(dead_code)] pub fn get() -> T where ::Err: Debug, { let mut line: String = String::new(); stdin().read_line(&mut line).unwrap(); line.trim().parse().unwrap() } #[allow(dead_code)] pub fn gets() -> Vec where ::Err: Debug, { let mut line: String = String::new(); stdin().read_line(&mut line).unwrap(); line.split_whitespace() .map(|t| t.parse().unwrap()) .collect() } #[allow(dead_code)] pub fn get2() -> (T, U) where ::Err: Debug, ::Err: Debug, { let mut line: String = String::new(); stdin().read_line(&mut line).unwrap(); let mut iter = line.split_whitespace(); ( iter.next().unwrap().parse().unwrap(), iter.next().unwrap().parse().unwrap(), ) } } #[allow(unused_macros)] macro_rules! debug { ($x: expr) => { println!("{}: {:?}", stringify!($x), $x) } } struct UFT { pub par: Vec, pub num: Vec, pub rank: Vec, } impl UFT { fn new(n: usize) -> Self { UFT { par: (0..n).collect(), num: vec![1; n], rank: vec![0; n], } } fn find(&mut self, x: usize) -> usize { if self.par[x] == x { x } else { let p = self.par[x]; let pp = self.find(p); self.par[x] = pp; pp } } fn unite(&mut self, x: usize, y: usize) { let x = self.find(x); let y = self.find(y); if x == y { return; } if (self.num[x], y) > (self.num[y], x) { self.par[y] = x; self.num[x] += self.num[y]; self.num[y] = 0; } else { self.par[x] = y; self.num[y] += self.num[x]; self.num[x] = 0; } /* if self.rank[x] < self.rank[y] { self.par[x] = y; } else { self.par[y] = x; if self.rank[x] == self.rank[y] { self.rank[x] += 1; } } */ } } fn main() { let (n, m): (usize, usize) = util::get2(); let ab: Vec<(usize, usize)> = (0..m).map(|_| util::get2()).collect(); let mut uft = UFT::new(n + 1); for &(a, b) in &ab { uft.unite(a, b); } for i in 1..n + 1 { println!("{}", uft.find(i)); } }