結果

問題 No.1565 Union
ユーザー 👑 箱
提出日時 2021-06-26 13:11:14
言語 Kotlin
(1.9.23)
結果
AC  
実行時間 865 ms / 2,000 ms
コード長 1,229 bytes
コンパイル時間 12,211 ms
コンパイル使用メモリ 438,244 KB
実行使用メモリ 89,608 KB
最終ジャッジ日時 2023-12-30 15:41:51
合計ジャッジ時間 31,335 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 265 ms
51,864 KB
testcase_01 AC 267 ms
51,860 KB
testcase_02 AC 265 ms
51,860 KB
testcase_03 AC 268 ms
51,864 KB
testcase_04 AC 266 ms
51,872 KB
testcase_05 AC 267 ms
51,876 KB
testcase_06 AC 270 ms
51,896 KB
testcase_07 AC 269 ms
51,888 KB
testcase_08 AC 265 ms
51,892 KB
testcase_09 AC 267 ms
51,880 KB
testcase_10 AC 531 ms
64,040 KB
testcase_11 AC 619 ms
74,376 KB
testcase_12 AC 613 ms
70,332 KB
testcase_13 AC 466 ms
64,000 KB
testcase_14 AC 791 ms
76,468 KB
testcase_15 AC 748 ms
78,804 KB
testcase_16 AC 709 ms
78,592 KB
testcase_17 AC 858 ms
80,980 KB
testcase_18 AC 865 ms
80,980 KB
testcase_19 AC 817 ms
80,824 KB
testcase_20 AC 693 ms
89,544 KB
testcase_21 AC 709 ms
89,544 KB
testcase_22 AC 701 ms
89,536 KB
testcase_23 AC 707 ms
89,608 KB
testcase_24 AC 677 ms
82,312 KB
testcase_25 AC 725 ms
89,536 KB
testcase_26 AC 666 ms
81,324 KB
testcase_27 AC 684 ms
81,332 KB
testcase_28 AC 698 ms
89,528 KB
testcase_29 AC 709 ms
89,544 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.PrintWriter
import java.util.*

fun PrintWriter.solve() {
    val n = nextInt()
    val m = nextInt()
    val adj = Array(n) { mutableListOf<Int>() }
    for (i in 0 until m) {
        val v1 = nextInt() - 1
        val v2 = nextInt() - 1
        adj[v1].add(v2)
        adj[v2].add(v1)
    }
    val que: Queue<Pair<Int, Int>> = ArrayDeque()
    val visited = BooleanArray(n) { false }
    visited[0] = true
    que.add(0 to 0)
    while (que.isNotEmpty()) {
        val (v, s) = que.poll()
        if (v == n - 1) {
            println(s)
            return
        }
        for (w in adj[v]) {
            if (!visited[w]) {
                que.add(w to s + 1)
                visited[w] = true
            }
        }
    }
    println(-1)
}

fun main() {
    val writer = PrintWriter(System.out, false)
    writer.solve()
    writer.flush()
}

// region Scanner
private var st = StringTokenizer("")
private val br = System.`in`.bufferedReader()

fun next(): String {
    while (!st.hasMoreTokens()) st = StringTokenizer(br.readLine())
    return st.nextToken()
}

fun nextInt() = next().toInt()
fun nextLong() = next().toLong()
fun nextLine() = br.readLine()
fun nextDouble() = next().toDouble()
// endregion
0