https://www.acmicpc.net/submit/1260/48093515
import java.lang.Math.*
import java.util.*
import kotlin.collections.ArrayList
import kotlin.collections.HashMap
private val br = System.`in`.bufferedReader()
fun bfs(start: Int){
var q = ArrayDeque<Int>()
q.add(start)
visit[start] = true
print("$start ")
while(!q.isEmpty()){
val curr = q.poll()
graph[curr].forEach{ nextNode ->
if (!visit[nextNode]){
visit[nextNode] = true
q.add(nextNode)
print("$nextNode ")
}
}
}
}
fun dfs(curr: Int){
visit[curr] = true
print("$curr ")
graph[curr].forEach{ nextNode ->
if(!visit[nextNode]){
visit[nextNode] = true
dfs(nextNode)
}
}
}
var graph = mutableListOf<MutableList<Int>>()
var visit = booleanArrayOf()
fun main() {
val (n, m, v) = br.readLine().split(' ').map { it.toInt() }
graph = MutableList(n+1) { mutableListOf<Int>() }
visit = BooleanArray(n+1)
repeat(m){
val (y,x) = br.readLine().split(' ').map{it.toInt()}
graph[y].add(x)
graph[x].add(y)
}
for(i in 1 until n+1){
graph[i].sort()
}
dfs(v)
println()
visit.fill(false)
bfs(v)
}