use std::collections::HashSet; fn dfs(cur: usize, prev: usize, paths: &Vec>, depth: &mut usize, dists: &mut HashSet) { let mut is_leaf = true; for &(v, _) in paths[cur].iter() { if v == prev { continue; } *depth += 1; dfs(v, cur, paths, depth, dists); *depth -= 1; is_leaf = false; } if is_leaf { dists.insert(*depth); } } 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]; 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, 1)); paths[b].push((a, 1)); } let size = paths.iter().filter(|&v| v.len() >= 3).count(); if size > 2 { println!("No"); } else if size == 0 { println!("Yes"); } else { let idx = paths.iter().enumerate().filter(|&(i, v)| v.len() >= 3).nth(0).unwrap().0; let mut leaf_dists = HashSet::new(); let mut depth = 0usize; dfs(idx, idx, &paths, &mut depth, &mut leaf_dists); if leaf_dists.len() == 1 { println!("Yes"); } else { println!("No"); } } }