Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package sprint7.tasks
- import java.io.BufferedReader
- import java.io.InputStreamReader
- import java.util.*
- const val M = 1_000_000_007
- fun main() {
- val buffer = BufferedReader(InputStreamReader(System.`in`))
- val (vertexCount, edgeCount) = buffer.readLine().split(" ").map { it.toInt() }
- val graph = Array(vertexCount + 1) { mutableListOf<Int>() }
- repeat(edgeCount) {
- val (vertex, toVertex) = buffer.readLine().split(" ").map { it.toInt() }
- graph[vertex].add(toVertex)
- }
- val (start, end) = buffer.readLine().split(" ").map { it.toInt() }
- val queue = LinkedList<Int>().apply { push(start) }
- val foundPaths = IntArray(vertexCount + 1) { 0 }.also { it[start] = 1 }
- while (queue.isNotEmpty()) {
- val cur = queue.poll()
- if (cur == end) {
- continue
- }
- graph[cur].forEach {
- if (foundPaths[it] == 0) {
- queue.add(it)
- }
- foundPaths[it] = (foundPaths[it] + foundPaths[cur]) % M
- }
- }
- println(foundPaths[end])
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement