結果

問題 No.1813 Magical Stones
ユーザー yudedakoyudedako
提出日時 2022-01-30 23:42:14
言語 Scala(Beta)
(3.4.0)
結果
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
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,115 ms
63,480 KB
testcase_01 AC 1,076 ms
63,476 KB
testcase_02 AC 1,096 ms
63,464 KB
testcase_03 AC 1,433 ms
72,088 KB
testcase_04 AC 1,111 ms
63,624 KB
testcase_05 AC 1,085 ms
63,580 KB
testcase_06 AC 1,245 ms
64,136 KB
testcase_07 AC 1,124 ms
63,864 KB
testcase_08 AC 1,147 ms
64,028 KB
testcase_09 AC 1,154 ms
64,100 KB
testcase_10 AC 1,161 ms
64,120 KB
testcase_11 AC 1,778 ms
70,228 KB
testcase_12 AC 1,110 ms
63,780 KB
testcase_13 AC 1,126 ms
63,664 KB
testcase_14 AC 1,376 ms
70,232 KB
testcase_15 TLE -
testcase_16 TLE -
testcase_17 TLE -
testcase_18 TLE -
testcase_19 TLE -
testcase_20 TLE -
testcase_21 TLE -
testcase_22 TLE -
testcase_23 TLE -
testcase_24 AC 1,196 ms
64,120 KB
testcase_25 AC 1,627 ms
67,964 KB
testcase_26 AC 1,561 ms
65,164 KB
testcase_27 TLE -
testcase_28 TLE -
testcase_29 TLE -
testcase_30 TLE -
testcase_31 TLE -
testcase_32 AC 1,102 ms
63,728 KB
testcase_33 AC 1,102 ms
63,592 KB
testcase_34 AC 1,458 ms
65,028 KB
testcase_35 AC 1,521 ms
66,828 KB
testcase_36 AC 1,212 ms
64,304 KB
testcase_37 AC 1,682 ms
71,216 KB
testcase_38 AC 1,712 ms
70,104 KB
testcase_39 TLE -
testcase_40 AC 1,314 ms
64,564 KB
testcase_41 AC 1,520 ms
65,112 KB
testcase_42 AC 1,505 ms
67,868 KB
testcase_43 AC 1,542 ms
66,448 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import scala.collection.mutable
import scala.collection.mutable.ArrayBuffer
import scala.io.StdIn.*
import scala.math.*
import scala.util.chaining.*
object DAG:
  def dag(graph: Array[ArrayBuffer[Int]]): Array[Int] =
    val n = graph.length
    val indices = Array.fill(n){0}
    val stack = Array.fill(n){0}
    val size = Array.fill(n){0}
    var pos = 0
    var outPos = n - 1
    for i <- 0 until n do
      if indices(i) == 0 then
        stack(pos) = i
        pos += 1
        while pos > 0 do
          pos -= 1
          val top = stack(pos)
          val edge = graph(top)
          if indices(top) == edge.size then
            indices(top) = -1
            stack(outPos) = top
            outPos -= 1
          else
            var index = indices(top)
            while index < edge.length && indices(edge(index)) != 0 do
              size(edge(index)) += 1
              index += 1
            pos += 1
            if index < edge.size then
              stack(pos) = edge(index)
              pos += 1
              size(edge(index)) += 1
              index += 1
            indices(top) = index
    val reverseGraph = Array.tabulate(n){i => Array.fill(size(i)){0}}
    for from <- graph.indices do
      for to <- graph(from) do
        size(to) -= 1
        reverseGraph(to)(size(to)) = from
    val newStack = size
    val result = indices
    for i <- stack do
      if result(i) < 0 then
        outPos += 1
        result(i) = outPos
        newStack(pos) = i
        pos += 1
        while pos > 0 do
          pos -= 1
          val from = newStack(pos)
          for to <- reverseGraph(from) do
            if result(to) < 0 then
              result(to) = outPos
              size(pos) = to
              pos += 1
    result
@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 do
    graph(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 do
    val a = dag(i)
    val b = dag(j)
    if a != b then
      outDegree(a) += 1
      inDegree(b) += 1
  val greatest = inDegree.count(_ == 0)
  val smallest = outDegree.count(_ == 0)
  if inDegree.length == 1 then
    println(0)
  else
    println(max(greatest, smallest))

0