2026-04-07 21:01:20 +09:00

35 lines
877 B
Swift

func main() {
guard let N = Int(readLine() ?? "") else { return }
var graph: [[Int]] = Array(repeating: [], count: N+1)
for _ in 1..<N {
guard let input = readLine() else { return }
let uv = input.split(separator: " ").compactMap{Int($0)}
graph[uv[0]].append(uv[1])
graph[uv[1]].append(uv[0])
}
var visited: [Bool] = Array(repeating: false, count: N+1)
var dp: [[Int]] = Array(repeating: Array(repeating: 0, count: 2), count: N+1)
func find(_ x: Int) {
visited[x] = true
dp[x][0] = 1
for child in graph[x] {
if visited[child] { continue }
find(child)
dp[x][0] += min(dp[child][0], dp[child][1])
dp[x][1] += dp[child][0]
}
}
find(1)
print(min(dp[1][0], dp[1][1]))
}
main()