struct Scanner { private var elements = [String]() private var index = 0 mutating func peek() -> String { while elements.count == index { elements = readLine()!.split(separator: " ").map(String.init) index = 0 } return elements[index] } mutating func next() -> String { defer { index += 1 } return peek() } mutating func nextInt() -> Int { return Int(next())! } mutating func nextInts(_ n: Int) -> [Int] { return (0 ..< n).map { _ in nextInt() } } mutating func nextDouble() -> Double { return Double(next())! } } infix operator &&=: AssignmentPrecedence func &&= (lhs: inout Bool, rhs: Bool) { lhs = lhs && rhs } var scanner = Scanner() let N = scanner.nextInt() var G = [[Int]](repeating: [], count: N) for _ in 0 ..< N - 1 { let a = scanner.nextInt() - 1 let b = scanner.nextInt() - 1 G[a].append(b) G[b].append(a) } for i in 0 ..< N { G[i].sort() } func check(_ r: Int) -> Bool { var L = [Bool](repeating: true, count: N) var R = [Bool](repeating: true, count: N) var first = [Int](repeating: Int.max, count: N) func dfs(_ x: Int, _ p: Int?) { let children = G[x].filter { $0 != p } guard !children.isEmpty else { return } first[x] = children.first! for c in children { dfs(c, x) } L[x] &&= children.allSatisfy { L[$0] && first[$0] > x } if p == nil { R[x] &&= (children[0 ..< children.count - 1].allSatisfy { L[$0] && first[$0] > x } && R[children.last!]) || (children[1 ..< children.count].allSatisfy { L[$0] && first[$0] > x } && R[children.first!]) } else { R[x] &&= (children[0 ..< children.count - 1].allSatisfy { L[$0] && first[$0] > x } && R[children.last!]) } } dfs(r, nil) return R[r] } for i in 0 ..< N { print(check(i) ? "Yes" : "No") }