Advertisement
vkazar

Untitled

Dec 17th, 2023
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 1.07 KB | None | 0 0
  1. package sprint7.tasks
  2.  
  3. import java.io.BufferedReader
  4. import java.io.InputStreamReader
  5. import java.util.*
  6.  
  7. const val M = 1_000_000_007
  8.  
  9. fun main() {
  10.  
  11.     val buffer = BufferedReader(InputStreamReader(System.`in`))
  12.     val (vertexCount, edgeCount) = buffer.readLine().split(" ").map { it.toInt() }
  13.     val graph = Array(vertexCount + 1) { mutableListOf<Int>() }
  14.  
  15.     repeat(edgeCount) {
  16.         val (vertex, toVertex) = buffer.readLine().split(" ").map { it.toInt() }
  17.  
  18.         graph[vertex].add(toVertex)
  19.     }
  20.     val (start, end) = buffer.readLine().split(" ").map { it.toInt() }
  21.  
  22.     val queue = LinkedList<Int>().apply { push(start) }
  23.     val foundPaths = IntArray(vertexCount + 1) { 0 }.also { it[start] = 1 }
  24.  
  25.     while (queue.isNotEmpty()) {
  26.         val cur = queue.poll()
  27.         if (cur == end) {
  28.             continue
  29.         }
  30.         graph[cur].forEach {
  31.             if (foundPaths[it] == 0) {
  32.                 queue.add(it)
  33.             }
  34.             foundPaths[it] = (foundPaths[it] + foundPaths[cur]) % M
  35.         }
  36.     }
  37.     println(foundPaths[end])
  38. }
  39.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement