use std::io::*; fn dfs(v: usize, p: usize, g: &Vec>) -> (usize, usize) { let mut res = (v, 0); for &nv in g[v].iter() { if nv != p { let mut tmp = dfs(nv, v, g); tmp.1 += 1; if tmp.1 > res.1 { res = tmp; } } } res } fn calc_dep(v: usize, p: usize, d: usize, dep: &mut Vec, g: &Vec>) { dep[v] = d; for &nv in g[v].iter() { if nv != p { calc_dep(nv, v, d + 1, dep, g); } } } fn main() { let mut s: String = String::new(); std::io::stdin().read_to_string(&mut s).ok(); let mut itr = s.trim().split_whitespace(); let n: usize = itr.next().unwrap().parse().unwrap(); let mut g: Vec> = vec![Vec::new(); n]; let mut deg: Vec = vec![0; n]; for _ in 0..n - 1 { let a: usize = itr.next().unwrap().parse::().unwrap() - 1; let b: usize = itr.next().unwrap().parse::().unwrap() - 1; deg[a] += 1; deg[b] += 1; g[a].push(b); g[b].push(a); } let mut leaf = Vec::new(); let res1 = dfs(0, 0, &g); let res2 = dfs(res1.0, n, &g); for i in 0..n { if deg[i] == 1 { leaf.push(i); } } let mut dep = vec![0; n]; calc_dep(res1.0, n, 0, &mut dep, &g); let mut ok = true; dep[res1.0] = res2.1; for i in 1..leaf.len() { ok &= dep[leaf[i]] == dep[leaf[0]]; } calc_dep(res2.0, n, 0, &mut dep, &g); dep[res2.0] = res2.1; for i in 1..leaf.len() { ok &= dep[leaf[i]] == dep[leaf[0]]; } if ok { println!("Yes") } else { println!("No") } }