use std::collections::VecDeque; fn dfs(cur: usize, prev: usize, paths: &Vec>, parents: &mut Vec) { parents[cur] = prev; for &v in paths[cur].iter() { if v == prev { continue; } dfs(v, cur, paths, parents); } } fn main() { let mut n = String::new(); std::io::stdin().read_line(&mut n).ok(); let n: usize = n.trim().parse().unwrap(); let mut paths = vec![vec![]; n]; let mut lines = Vec::with_capacity(n-1); for _ in 0..n-1 { let mut ab = String::new(); std::io::stdin().read_line(&mut ab).ok(); let ab: Vec = ab.trim().split_whitespace().map(|s| s.parse().unwrap()).collect(); let a = ab[0]-1; let b = ab[1]-1; paths[a].push(b); paths[b].push(a); lines.push((a, b)); } let mut parents = vec![n-1; n]; dfs(n-1, n-1, &paths, &mut parents); let mut vals = vec![0isize; n]; for &(a, b) in lines.iter() { if a > b { if parents[b] == a { vals[b] -= 1; vals[n-1] += 1; } else { vals[a] += 1; } } else { if parents[a] == b { vals[a] -= 1; vals[n-1] += 1; } else { vals[b] += 1; } } } let mut result = vec![0isize; n]; result[n-1] = vals[n-1]; let mut deque = VecDeque::new(); deque.push_back((n-1, vals[n-1])); while let Some((u, val)) = deque.pop_front() { for &v in paths[u].iter() { if v == parents[u] { continue; } let nval = val + vals[v]; result[v] = nval; deque.push_back((v, nval)); } } for &v in result.iter() { println!("{}", v); } }