-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution.kt
32 lines (29 loc) · 914 Bytes
/
solution.kt
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
import java.util.*
fun main(args: Array<out String>) {
val n = readLine()!!.toInt()
val adjacencyMatrix = MutableList(n) {
readLine()!!.split(" ").map { it == "1" }.toMutableList()
}
for (i in 0 until n) {
val visited = HashSet<Int>()
val toVisit = ArrayDeque<Int>()
toVisit.addFirst(i)
while (toVisit.isNotEmpty()) {
val curr = toVisit.removeFirst()
for (next in 0 until n) {
if (next in visited) {
continue
}
if (!adjacencyMatrix[curr][next]) {
continue
}
toVisit.addFirst(next)
adjacencyMatrix[i][next] = true
visited.add(next)
}
}
}
adjacencyMatrix.joinToString("\n") {
it.joinToString(" ") { if (it) "1" else "0" }
}.let(::println)
}