Last active
October 15, 2019 08:36
-
-
Save naoppy/ae508fdec3a6f573d81148791e7c4ee2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.* | |
import kotlin.collections.ArrayList | |
private data class Edge(val to: Int) | |
fun main() { | |
val n = 10 | |
val edges = Array(n) { | |
ArrayList<Edge>() | |
} | |
edges[0].addAll(listOf(2, 4, 5).map { Edge(it) }) | |
edges[1].addAll(listOf(5).map { Edge(it) }) | |
edges[2].addAll(listOf(0, 3).map { Edge(it) }) | |
edges[3].addAll(listOf(2, 8, 9).map { Edge(it) }) | |
edges[4].addAll(listOf(0, 6, 7).map { Edge(it) }) | |
edges[5].addAll(listOf(0, 1).map { Edge(it) }) | |
edges[6].addAll(listOf(0, 4, 7).map { Edge(it) }) | |
edges[7].addAll(listOf(4, 6).map { Edge(it) }) | |
edges[8].addAll(listOf(3).map { Edge(it) }) | |
edges[9].addAll(listOf(3).map { Edge(it) }) | |
val queue = ArrayDeque<Int>() | |
queue.add(5) | |
val used = BooleanArray(n) { false } | |
while (queue.isNotEmpty()) { | |
val v = queue.poll() | |
if(used[v]) continue | |
used[v] = true | |
for (edge in edges[v]) { | |
if(used[edge.to]) continue | |
queue.add(edge.to) | |
} | |
} | |
if(used[9]) { | |
println("9 found") | |
} else { | |
println("Not found") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment