pub mod scanner { pub struct Scanner { buf: Vec, } impl Scanner { pub fn new() -> Self { Self { buf: vec![] } } pub fn new_from(source: &str) -> Self { let source = String::from(source); let buf = Self::split(source); Self { buf } } pub fn next(&mut self) -> T { loop { if let Some(x) = self.buf.pop() { return x.parse().ok().expect(""); } let mut source = String::new(); std::io::stdin().read_line(&mut source).expect(""); self.buf = Self::split(source); } } fn split(source: String) -> Vec { source .split_whitespace() .rev() .map(String::from) .collect::>() } } } pub mod tree { #[derive(Clone)] pub struct Edge { pub to: usize, pub id: usize, pub rev: usize, pub cost: isize, } pub struct Tree { pub n: usize, root: usize, pub edges: Vec>, pub parent: Vec>, pub depth: Vec, pub subtree_size: Vec, pub pre_order: Vec, pub post_order: Vec, pub euler_tour: Vec, } impl Tree { pub fn new(n: usize) -> Tree { Tree { n, root: 0, edges: vec![vec![]; n], parent: vec![None; n], depth: vec![0; n], subtree_size: vec![0; n], pre_order: vec![], post_order: vec![], euler_tour: vec![], } } pub fn init(scanner: &mut crate::scanner::Scanner, indexed: usize, weighted: bool) -> Tree { let n: usize = scanner.next(); let mut tree = Tree::new(n); for id in 0..n - 1 { let u: usize = scanner.next::() - indexed; let v: usize = scanner.next::() - indexed; let cost: isize; if weighted { cost = scanner.next(); } else { cost = 1; } tree.add_edge(u, v, id, cost); } tree.set_root(0); tree } pub fn add_edge(&mut self, u: usize, v: usize, id: usize, cost: isize) { { let to = v; let rev = self.edges[v].len(); self.edges[u].push(Edge { to, id, rev, cost }); } { let to = u; let rev = self.edges[u].len() - 1; self.edges[v].push(Edge { to, id, rev, cost }); } } pub fn degree(&self, u: usize) -> usize { self.edges[u].len() } pub fn set_root(&mut self, root: usize) { let n = self.n; self.root = root; self.parent = vec![None; n]; self.depth = vec![0; n]; self.subtree_size = vec![0; n]; self.pre_order = vec![]; self.post_order = vec![]; self.euler_tour = vec![]; let mut stack: Vec<(usize, usize, usize)> = vec![]; stack.push((root, 0, 0)); while let Some((u, i, d)) = stack.pop() { if i == 0 { self.subtree_size[u] += 1; self.pre_order.push(u); } if i == self.degree(u) { if let Some(p) = self.parent[u] { self.subtree_size[p] += self.subtree_size[u]; } self.post_order.push(u); self.euler_tour.push(u); continue; } stack.push((u, i + 1, d)); let edge = &self.edges[u][i]; if Some(edge.to) == self.parent[u] { continue; } self.parent[edge.to] = Some(u); self.depth[edge.to] = d + 1; self.euler_tour.push(u); stack.push((edge.to, 0, d + 1)); } } } } use crate::{scanner::Scanner, tree::Tree}; use std::io::Write; fn main() { let mut scanner = Scanner::new(); let out = std::io::stdout(); let mut out = std::io::BufWriter::new(out.lock()); let t: usize = 1; for _ in 0..t { solve(&mut scanner, &mut out); } } fn solve(scanner: &mut Scanner, out: &mut std::io::BufWriter) { let n: usize = scanner.next(); let mut tree = Tree::new(n); for id in 0..n - 1 { let u: usize = scanner.next::() - 1; let v: usize = scanner.next::() - 1; tree.add_edge(u, v, id, 1); } tree.set_root(0); for u in 0..n { let mut ans = 0; for edge in tree.edges[u].iter() { let v = edge.to; ans += tree.edges[v].len() - 1; } writeln!(out, "{}", ans).unwrap(); } }