結果
問題 | No.1817 Reversed Edges |
ユーザー | yudedako |
提出日時 | 2022-01-25 18:03:56 |
言語 | Scala(Beta) (3.4.0) |
結果 |
AC
|
実行時間 | 1,421 ms / 2,000 ms |
コード長 | 1,365 bytes |
コンパイル時間 | 14,869 ms |
コンパイル使用メモリ | 261,332 KB |
実行使用メモリ | 85,984 KB |
最終ジャッジ日時 | 2024-05-09 18:44:54 |
合計ジャッジ時間 | 44,387 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 786 ms
63,616 KB |
testcase_01 | AC | 798 ms
63,576 KB |
testcase_02 | AC | 783 ms
63,660 KB |
testcase_03 | AC | 786 ms
63,732 KB |
testcase_04 | AC | 783 ms
63,512 KB |
testcase_05 | AC | 777 ms
63,672 KB |
testcase_06 | AC | 779 ms
63,672 KB |
testcase_07 | AC | 1,346 ms
83,048 KB |
testcase_08 | AC | 1,216 ms
73,412 KB |
testcase_09 | AC | 1,372 ms
82,500 KB |
testcase_10 | AC | 1,253 ms
75,212 KB |
testcase_11 | AC | 1,319 ms
77,708 KB |
testcase_12 | AC | 1,379 ms
85,212 KB |
testcase_13 | AC | 1,392 ms
85,716 KB |
testcase_14 | AC | 1,375 ms
85,760 KB |
testcase_15 | AC | 1,382 ms
85,404 KB |
testcase_16 | AC | 1,371 ms
85,456 KB |
testcase_17 | AC | 1,421 ms
85,612 KB |
testcase_18 | AC | 1,389 ms
85,300 KB |
testcase_19 | AC | 1,403 ms
85,480 KB |
testcase_20 | AC | 1,390 ms
85,316 KB |
testcase_21 | AC | 1,374 ms
85,352 KB |
testcase_22 | AC | 1,338 ms
84,232 KB |
testcase_23 | AC | 1,373 ms
83,412 KB |
testcase_24 | AC | 1,402 ms
85,984 KB |
ソースコード
import scala.collection.mutable import scala.io.StdIn.* import scala.math.{min, max} @main def main = val n = readLine().toInt val edges = Array.fill(n - 1){ val Array(u, v) = readLine().trim().split(' ').map(_.toInt - 1) (min(u, v), max(u, v)) } val graph = Array.fill(n){Nil: List[Int]} for (u, v) <- edges do graph(u) ::= v graph(v) ::= u val depth = Array.fill(graph.length){-1} depth(0) = 0 val parent = Array.fill(graph.length){-1} val queue = mutable.Queue(0) while queue.nonEmpty do val current = queue.dequeue() for next <- graph(current) do if depth(next) == -1 then depth(next) = depth(current) + 1 queue.append(next) parent(next) = current var currentSum = edges.count((u, v) => depth(u) > depth(v)) val stack = mutable.Stack(0) val result = Array.fill(n){-1} val state = graph.clone() while stack.nonEmpty do val current = stack.pop() state(current) match case Nil => result(current) = currentSum if parent(current) >= 0 then currentSum += (if current > parent(current) then -1 else 1) case next::t => stack.push(current) state(current) = t if depth(current) < depth(next) then currentSum += (if current > next then -1 else 1) stack.push(next) println(result.mkString("\n"))