백준 - 24480 알고리즘 수업 - 깊이 우선 탐색 2
문제
24480 알고리즘 수업 - 깊이 우선 탐색 2
답
kotlin code
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
var visited = intArrayOf()
var order = 0
fun main() {
val inputs = readln().split(" ").map{ it.toInt() }
val pointCount = inputs[0]
val lineCount = inputs[1]
val startPoint = inputs[2] - 1
val lines = Array<MutableList<Int>>(pointCount) { mutableListOf() }
repeat(lineCount) {
readln().split(" ").map { it.toInt() - 1 }.let {
lines[it.first()].add(it.last())
lines[it.last()].add(it.first())
}
}
lines.forEach { it.sortDescending() }
visited = IntArray(pointCount) { order }
dfs(lines, startPoint)
visited.forEach { println(it) }
}
fun dfs(lines: Array<MutableList<Int>>, startPoint: Int) {
visited[startPoint] = ++order
lines[startPoint].forEach {
if(visited[it] == 0) dfs(lines, it)
}
}
This post is licensed under CC BY 4.0 by the author.