import java.io.BufferedReader import java.io.InputStream import java.io.InputStreamReader import java.io.PrintWriter import java.util.* fun PrintWriter.solve(sc: FastScanner) { val n = sc.nextInt() val adj = Array(n) { mutableSetOf() } for (i in 0 until n - 1) { val v1 = sc.nextInt() - 1 val v2 = sc.nextInt() - 1 adj[v1].add(v2) adj[v2].add(v1) } val que: Queue> = ArrayDeque() que.add(Triple(0, -1, 0)) val dist = Array(n) { 0 } while (que.count() > 0) { val (v, p, d) = que.poll() dist[v] = d for (w in adj[v]) { if (w != p) { que.add(Triple(w, v, d + 1)) } } } val max = dist.max()!! val v0 = (0 until n).first { dist[it] == max } que.add(Triple(v0, -1, 0)) while (que.count() > 0) { val (v, p, d) = que.poll() dist[v] = d for (w in adj[v]) { if (w != p) { que.add(Triple(w, v, d + 1)) } } } val diam = dist.max()!! que.add(Triple(0, -1, 0)) val leaves = mutableListOf>() while (que.count() > 0) { val (v, p, d) = que.poll() dist[v] = d var isleaf = true for (w in adj[v]) { if (w != p) { isleaf = false que.add(Triple(w, v, d + 1)) } } if (isleaf) { leaves.add(v to d) } } val (v1, min) = leaves.minBy { it.second }!! que.add(Triple(v1, -1, 0)) leaves.clear() while (que.count() > 0) { val (v, p, d) = que.poll() dist[v] = d var isleaf = true for (w in adj[v]) { if (w != p) { isleaf = false que.add(Triple(w, v, d + 1)) } } if (isleaf) { leaves.add(v to d) } } val l = leaves.map { it.second }.min()!! println(if (diam == l) "Yes" else "No") } fun main() { val writer = PrintWriter(System.out, false) writer.solve(FastScanner(System.`in`)) writer.flush() } class FastScanner(s: InputStream) { private var st = StringTokenizer("") private val br = BufferedReader(InputStreamReader(s)) fun next(): String { while (!st.hasMoreTokens()) st = StringTokenizer(br.readLine()) return st.nextToken() } fun nextInt() = next().toInt() fun nextLong() = next().toLong() fun nextLine() = br.readLine() fun nextDouble() = next().toDouble() fun ready() = br.ready() }