struct DFS { graph: Vec>, } impl DFS { fn new(n: usize) -> DFS { DFS { graph: vec![vec![]; n], } } fn add_edge(&mut self, from: usize, to: usize) { self.graph[from].push(to); } fn search(&mut self, from: usize, d: usize) -> usize { let mut res = 0; for &to in self.graph[from].clone().iter() { res += self.search(to, d + 1); } res + (1 + d) * d / 2 } } fn main() { let n: usize = { let mut buf = String::new(); std::io::stdin().read_line(&mut buf).unwrap(); buf.trim_end().parse().unwrap() }; let mut dfs = DFS::new(n); let mut is_root = vec![true; n]; for _ in 0..(n - 1) { let (a, b): (usize, usize) = { let mut buf = String::new(); std::io::stdin().read_line(&mut buf).unwrap(); let mut iter = buf.split_whitespace(); ( iter.next().unwrap().parse::().unwrap() - 1, iter.next().unwrap().parse::().unwrap() - 1, ) }; dfs.add_edge(a, b); is_root[b] = false; } let root = { is_root.iter() .enumerate() .filter(|&(_, &b)| b) .map(|(i, _)| i) .next() .unwrap() }; let ans = dfs.search(root, 0); println!("{}", ans); }