結果
| 問題 |
No.1295 木と駒
|
| コンテスト | |
| ユーザー |
semisagi
|
| 提出日時 | 2020-11-20 22:39:58 |
| 言語 | Swift (6.0.3) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 1,980 bytes |
| コンパイル時間 | 1,549 ms |
| コンパイル使用メモリ | 137,748 KB |
| 実行使用メモリ | 23,628 KB |
| 最終ジャッジ日時 | 2024-07-23 13:29:11 |
| 合計ジャッジ時間 | 5,966 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 7 WA * 6 TLE * 1 -- * 34 |
ソースコード
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")
}
semisagi