結果

問題 No.1660 Matrix Exponentiation
ユーザー 👑 箱星箱星
提出日時 2021-07-23 05:29:24
言語 Kotlin
(1.9.23)
結果
RE  
実行時間 -
コード長 2,831 bytes
コンパイル時間 11,854 ms
コンパイル使用メモリ 444,824 KB
実行使用メモリ 110,868 KB
最終ジャッジ日時 2024-05-01 01:00:42
合計ジャッジ時間 26,021 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 272 ms
57,892 KB
testcase_01 AC 250 ms
54,252 KB
testcase_02 AC 272 ms
57,892 KB
testcase_03 AC 265 ms
57,928 KB
testcase_04 AC 260 ms
57,908 KB
testcase_05 AC 244 ms
54,284 KB
testcase_06 AC 240 ms
54,432 KB
testcase_07 AC 241 ms
54,356 KB
testcase_08 AC 265 ms
57,896 KB
testcase_09 AC 400 ms
76,812 KB
testcase_10 AC 409 ms
78,876 KB
testcase_11 AC 252 ms
58,504 KB
testcase_12 AC 247 ms
54,348 KB
testcase_13 AC 244 ms
54,240 KB
testcase_14 AC 269 ms
57,832 KB
testcase_15 AC 295 ms
60,160 KB
testcase_16 AC 277 ms
60,104 KB
testcase_17 AC 267 ms
60,000 KB
testcase_18 AC 289 ms
60,260 KB
testcase_19 AC 652 ms
101,184 KB
testcase_20 AC 597 ms
92,864 KB
testcase_21 AC 646 ms
99,112 KB
testcase_22 AC 429 ms
73,068 KB
testcase_23 AC 523 ms
88,292 KB
testcase_24 RE -
testcase_25 AC 810 ms
108,256 KB
testcase_26 RE -
testcase_27 AC 258 ms
58,504 KB
testcase_28 AC 688 ms
109,756 KB
testcase_29 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

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

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 lst = topologicalSort(adj)
        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]
        }
        lst.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 topologicalSort(adj: Array<MutableList<Int>>): List<Int> {
    val ans = mutableListOf<Int>()
    val n = adj.size
    val ind = IntArray(n) { 0 }
    for (i in 0 until n) {
        for (v in adj[i]) ind[v]++
    }
    val que: Queue<Int> = ArrayDeque()
    for (i in 0 until n) {
        if (ind[i] == 0) que.add(i)
    }
    while (que.isNotEmpty()) {
        val now = que.poll()
        ans.add(now)
        for (v in adj[now]) {
            ind[v]--
            if (ind[v] == 0) que.add(v)
        }
    }
    return ans
}

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