use proconio::input; fn main() { input! { n:usize, m:usize, uvw:[(usize,usize,usize);m], t:usize, r:[usize;t], } let mut ids_sorted_r = (0..t).collect::>(); ids_sorted_r.sort_by_key(|&i| r[i]); let mut ids_sorted_w = (0..m).collect::>(); ids_sorted_w.sort_by_key(|&i| uvw[i].2); let mut ans = vec![0; t]; let mut uf = UnionFindTree::new(n); let mut id = 0; for &i in ids_sorted_r.iter() { while id < m && uvw[ids_sorted_w[id]].2 <= r[i] { let (u, v, _) = uvw[ids_sorted_w[id]]; uf.unite(u, v); id += 1; } ans[i] = uf.nofcc(); } for a in ans { println!("{}", a); } } use unionfindtree::*; mod unionfindtree { pub struct UnionFindTree { par: Vec, rank: Vec, size: Vec, } impl UnionFindTree { pub fn new(n: usize) -> Self { Self { par: (0..n).collect::>(), rank: vec![0; n], size: vec![1; n], } } #[allow(dead_code)] pub fn same(&mut self, v1: usize, v2: usize) -> bool { self.root(v1) == self.root(v2) } #[allow(dead_code)] pub fn size(&mut self, v: usize) -> usize { let v_root = self.root(v); self.size[v_root] } #[allow(dead_code)] pub fn root(&mut self, v: usize) -> usize { if self.par[v] != v { self.par[v] = self.root(self.par[v]); } self.par[v] } pub fn unite(&mut self, v1: usize, v2: usize) { let mut v1_root = self.root(v1); let mut v2_root = self.root(v2); if v1_root == v2_root { return; } if self.rank[v1_root] < self.rank[v2_root] { std::mem::swap(&mut v1_root, &mut v2_root); } self.par[v2_root] = v1_root; self.size[v1_root] += self.size[v2_root]; if self.rank[v1_root] == self.rank[v2_root] { self.rank[v1_root] += 1; } } #[allow(dead_code)] pub fn nofcc(&mut self) -> usize { let n = self.par.len(); (0..n) .map(|v| self.root(v)) .collect::>() .len() } } }