fn run<'a, F: FnMut() -> &'a str, W: std::io::Write>(scan: &mut F, writer: &mut W) { macro_rules! scan { ([$t:tt; $n:expr]) => ((0..$n).map(|_| scan!($t)).collect::>()); (($($t:tt),*)) => (($(scan!($t)),*)); (Usize1) => (scan!(usize) - 1); (Bytes) => (scan().as_bytes().to_vec()); ($t:ty) => (scan().parse::<$t>().unwrap()); } macro_rules! println { ($($arg:tt)*) => (writeln!(writer, $($arg)*).ok()); } let n = scan!(usize); let mut graph = vec![vec![]; n]; let mut degree = vec![0; n]; for _ in 0..(n - 1) { let (a, b) = scan!((Usize1, Usize1)); graph[a].push(b); graph[b].push(a); degree[a] += 1; degree[b] += 1; } let count = degree.iter().filter(|&&d| d >= 3).count(); if count >= 2 { println!("No"); return; } else if count == 0 { println!("Yes"); return; } let mut root = 0; for (i, &d) in degree.iter().enumerate() { if d >= 3 { root = i; } } let mut deque = std::collections::VecDeque::new(); deque.push_back(root); let mut dist = vec![0; n]; dist[root] = 1; while let Some(from) = deque.pop_front() { for &to in &graph[from] { if dist[to] == 0 { deque.push_back(to); dist[to] = dist[from] + 1; } } } let mut set = { degree.iter().enumerate() .filter(|(_, &d)| d == 1) .map(|(i, _)| dist[i]) .collect::>() }; set.dedup(); if set.len() == 1 { println!("Yes"); } else { println!("No"); } } fn main() { let ref mut buf = Vec::new(); std::io::Read::read_to_end(&mut std::io::stdin(), buf).ok(); let mut scanner = unsafe { std::str::from_utf8_unchecked(buf).split_ascii_whitespace() }; let ref mut scan = || scanner.next().unwrap(); let stdout = std::io::stdout(); let ref mut writer = std::io::BufWriter::new(stdout.lock()); run(scan, writer); }