結果
問題 | No.1813 Magical Stones |
ユーザー |
|
提出日時 | 2022-01-30 23:42:14 |
言語 | Scala(Beta) (3.6.2) |
結果 |
TLE
(最新)
AC
(最初)
|
実行時間 | - |
コード長 | 2,449 bytes |
コンパイル時間 | 14,321 ms |
コンパイル使用メモリ | 265,512 KB |
実行使用メモリ | 103,728 KB |
最終ジャッジ日時 | 2024-06-11 08:31:09 |
合計ジャッジ時間 | 92,782 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 25 TLE * 15 |
ソースコード
import scala.collection.mutableimport scala.collection.mutable.ArrayBufferimport scala.io.StdIn.*import scala.math.*import scala.util.chaining.*object DAG:def dag(graph: Array[ArrayBuffer[Int]]): Array[Int] =val n = graph.lengthval indices = Array.fill(n){0}val stack = Array.fill(n){0}val size = Array.fill(n){0}var pos = 0var outPos = n - 1for i <- 0 until n doif indices(i) == 0 thenstack(pos) = ipos += 1while pos > 0 dopos -= 1val top = stack(pos)val edge = graph(top)if indices(top) == edge.size thenindices(top) = -1stack(outPos) = topoutPos -= 1elsevar index = indices(top)while index < edge.length && indices(edge(index)) != 0 dosize(edge(index)) += 1index += 1pos += 1if index < edge.size thenstack(pos) = edge(index)pos += 1size(edge(index)) += 1index += 1indices(top) = indexval reverseGraph = Array.tabulate(n){i => Array.fill(size(i)){0}}for from <- graph.indices dofor to <- graph(from) dosize(to) -= 1reverseGraph(to)(size(to)) = fromval newStack = sizeval result = indicesfor i <- stack doif result(i) < 0 thenoutPos += 1result(i) = outPosnewStack(pos) = ipos += 1while pos > 0 dopos -= 1val from = newStack(pos)for to <- reverseGraph(from) doif result(to) < 0 thenresult(to) = outPossize(pos) = topos += 1result@main def main =val Array(n, m) = readLine().split(' ').map(_.toInt)val edges = Array.fill(m){val Array(a, b) = readLine().split(' ').map(_.toInt - 1)a -> b}val graph = Array.fill(n){ArrayBuffer[Int]()}for (a, b) <- edges dograph(a).append(b)val dag = DAG.dag(graph)val inDegree = Array.fill(dag.max + 1){0}val outDegree = Array.fill(inDegree.length){0}for (i, j) <- edges doval a = dag(i)val b = dag(j)if a != b thenoutDegree(a) += 1inDegree(b) += 1val greatest = inDegree.count(_ == 0)val smallest = outDegree.count(_ == 0)if inDegree.length == 1 thenprintln(0)elseprintln(max(greatest, smallest))