結果

問題 No.1660 Matrix Exponentiation
ユーザー 👑 箱星箱星
提出日時 2021-07-23 08:17:46
言語 Kotlin
(1.9.23)
結果
AC  
実行時間 1,256 ms / 2,000 ms
コード長 2,354 bytes
コンパイル時間 16,141 ms
コンパイル使用メモリ 422,120 KB
実行使用メモリ 119,020 KB
最終ジャッジ日時 2023-09-05 11:29:14
合計ジャッジ時間 33,961 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 314 ms
60,000 KB
testcase_01 AC 281 ms
54,436 KB
testcase_02 AC 305 ms
56,352 KB
testcase_03 AC 305 ms
58,412 KB
testcase_04 AC 305 ms
56,284 KB
testcase_05 AC 278 ms
54,988 KB
testcase_06 AC 275 ms
54,612 KB
testcase_07 AC 274 ms
54,664 KB
testcase_08 AC 307 ms
58,544 KB
testcase_09 AC 399 ms
61,344 KB
testcase_10 AC 402 ms
61,356 KB
testcase_11 AC 287 ms
59,332 KB
testcase_12 AC 272 ms
52,584 KB
testcase_13 AC 270 ms
52,864 KB
testcase_14 AC 300 ms
58,228 KB
testcase_15 AC 328 ms
57,592 KB
testcase_16 AC 314 ms
58,200 KB
testcase_17 AC 319 ms
56,252 KB
testcase_18 AC 341 ms
57,260 KB
testcase_19 AC 812 ms
72,268 KB
testcase_20 AC 686 ms
73,196 KB
testcase_21 AC 809 ms
74,280 KB
testcase_22 AC 429 ms
61,344 KB
testcase_23 AC 628 ms
68,136 KB
testcase_24 AC 1,256 ms
117,656 KB
testcase_25 AC 924 ms
84,716 KB
testcase_26 AC 1,210 ms
115,528 KB
testcase_27 AC 290 ms
57,272 KB
testcase_28 AC 844 ms
85,248 KB
testcase_29 AC 1,008 ms
119,020 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.PrintWriter
import java.util.*
import kotlin.math.*

fun PrintWriter.solve() {
    val n = nextInt()
    val m = nextInt()
    val adj = Array(n) { mutableListOf<Int>() }
    val set = mutableSetOf<Pair<Int, Int>>()
    for (i in 0 until m) {
        val v1 = nextInt() - 1
        val v2 = nextInt() - 1
        if (v1 == v2 || set.contains(v2 to v1)) {
            println(-1)
            return
        }
        adj[v1].add(v2)
        set.add(v1 to v2)
    }
    if (findCycle(adj).isNotEmpty()) {
        println(-1)
    } else {
        val dp = IntArray(n) { -1 }
        fun dfs(v: Int): Int {
            if (dp[v] != -1) return dp[v]
            var max = 0
            for (w in adj[v]) {
                max = maxOf(max, dfs(w))
            }
            dp[v] = max + 1
            return dp[v]
        }
        (0 until n).forEach { dfs(it) }
        println(dp.maxOrNull()!!)
    }
}

fun findCycle(adj: Array<MutableList<Int>>): List<Int> {
    val n = adj.size
    val seen = BooleanArray(n) { false }
    val finished = BooleanArray(n) { false }
    var pos = -1
    val hist = Stack<Int>()
    fun dfs(v: Int, p: Int) {
        seen[v] = true
        hist.push(v)
        for (w in adj[v]) {
            if (w == p) continue
            if (finished[w]) continue
            if (seen[w] && !finished[w]) {
                pos = w
                return
            }
            dfs(w, v)
            if (pos != -1) return
        }
        hist.pop()
        finished[v] = true
    }
    for (i in 0 until n) {
        if (!finished[i]) {
            dfs(i, -1)
            if (pos != -1) break
        }
    }

    val cycle = mutableListOf<Int>()
    while (hist.isNotEmpty()) {
        val t = hist.pop()
        cycle.add(t)
        if (t == pos) break
    }
    return cycle
}

fun main() {
    Thread(null, {
        val writer = PrintWriter(System.out, false)
        writer.solve()
        writer.flush()
    }, "solve", 1.shl(26)).start()
}

// 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