結果
問題 | No.1660 Matrix Exponentiation |
ユーザー | 箱星 |
提出日時 | 2021-07-23 06:01:55 |
言語 | Kotlin (1.9.23) |
結果 |
AC
|
実行時間 | 1,160 ms / 2,000 ms |
コード長 | 3,054 bytes |
コンパイル時間 | 17,048 ms |
コンパイル使用メモリ | 450,784 KB |
実行使用メモリ | 146,796 KB |
最終ジャッジ日時 | 2024-09-27 09:20:28 |
合計ジャッジ時間 | 30,431 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 297 ms
62,044 KB |
testcase_01 | AC | 270 ms
58,492 KB |
testcase_02 | AC | 290 ms
62,140 KB |
testcase_03 | AC | 294 ms
62,036 KB |
testcase_04 | AC | 289 ms
62,052 KB |
testcase_05 | AC | 265 ms
58,668 KB |
testcase_06 | AC | 266 ms
58,456 KB |
testcase_07 | AC | 265 ms
58,492 KB |
testcase_08 | AC | 289 ms
62,052 KB |
testcase_09 | AC | 431 ms
74,708 KB |
testcase_10 | AC | 441 ms
81,128 KB |
testcase_11 | AC | 273 ms
60,536 KB |
testcase_12 | AC | 262 ms
58,600 KB |
testcase_13 | AC | 268 ms
58,420 KB |
testcase_14 | AC | 295 ms
62,088 KB |
testcase_15 | AC | 317 ms
62,256 KB |
testcase_16 | AC | 305 ms
62,188 KB |
testcase_17 | AC | 297 ms
62,136 KB |
testcase_18 | AC | 317 ms
62,332 KB |
testcase_19 | AC | 734 ms
101,096 KB |
testcase_20 | AC | 656 ms
92,968 KB |
testcase_21 | AC | 722 ms
101,068 KB |
testcase_22 | AC | 481 ms
77,164 KB |
testcase_23 | AC | 578 ms
94,424 KB |
testcase_24 | AC | 1,160 ms
146,628 KB |
testcase_25 | AC | 870 ms
110,240 KB |
testcase_26 | AC | 1,107 ms
146,796 KB |
testcase_27 | AC | 283 ms
60,560 KB |
testcase_28 | AC | 703 ms
111,848 KB |
testcase_29 | AC | 837 ms
139,956 KB |
ソースコード
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 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 main2() { val writer = PrintWriter(System.out, false) writer.solve() writer.flush() } fun main() = Thread(null, ::main2, "", 64 * 1024 * 1024) .apply { setUncaughtExceptionHandler { _, e -> e.printStackTrace(); kotlin.system.exitProcess(1) } } .apply { start() }.join() // 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