-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path11725번.swift
45 lines (39 loc) · 1.17 KB
/
11725번.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// 출처 : 백준 트리의 부모 찾기
// https://www.acmicpc.net/problem/11725
// 풀이 : hogumachu
import Foundation
func solution() {
let node = Int(readLine()!)!
var tree: [[Int]] = Array(repeating: [], count: node+1)
var parent: [Int] = Array(repeating: node+1, count: node+1)
var visited: [Bool] = Array(repeating: false, count: node+1)
var queue: [Int] = []
var index = 0
for _ in 1..<node {
let value = readLine()!.split(separator: " ").map{Int(String($0))!}
tree[value[0]].contains(value[1]) ? nil : tree[value[0]].append(value[1])
tree[value[1]].contains(value[0]) ? nil : tree[value[1]].append(value[0])
}
func visitNode(_ number: Int) -> Void {
for i in tree[number] {
if visited[i] == false {
visited[i] = true
parent[i] = number
queue.append(i)
}
}
}
for i in tree[1] {
parent[i] = 1
visited[i] = true
queue.append(i)
}
while queue.count > index {
visitNode(queue[index])
index += 1
}
for i in 2..<parent.count {
print(parent[i])
}
}
solution()