結果
問題 | No.1817 Reversed Edges |
ユーザー | yudedako |
提出日時 | 2022-01-25 18:03:56 |
言語 | Scala(Beta) (3.4.0) |
結果 |
AC
|
実行時間 | 1,702 ms / 2,000 ms |
コード長 | 1,365 bytes |
コンパイル時間 | 18,652 ms |
コンパイル使用メモリ | 262,988 KB |
実行使用メモリ | 85,904 KB |
最終ジャッジ日時 | 2024-12-16 10:16:03 |
合計ジャッジ時間 | 51,687 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 882 ms
63,472 KB |
testcase_01 | AC | 910 ms
63,684 KB |
testcase_02 | AC | 931 ms
63,560 KB |
testcase_03 | AC | 912 ms
63,676 KB |
testcase_04 | AC | 897 ms
63,604 KB |
testcase_05 | AC | 882 ms
63,732 KB |
testcase_06 | AC | 903 ms
63,580 KB |
testcase_07 | AC | 1,579 ms
82,656 KB |
testcase_08 | AC | 1,414 ms
74,092 KB |
testcase_09 | AC | 1,679 ms
82,368 KB |
testcase_10 | AC | 1,470 ms
75,480 KB |
testcase_11 | AC | 1,541 ms
77,360 KB |
testcase_12 | AC | 1,606 ms
85,172 KB |
testcase_13 | AC | 1,636 ms
85,704 KB |
testcase_14 | AC | 1,632 ms
85,184 KB |
testcase_15 | AC | 1,644 ms
85,404 KB |
testcase_16 | AC | 1,616 ms
85,320 KB |
testcase_17 | AC | 1,645 ms
85,708 KB |
testcase_18 | AC | 1,677 ms
85,380 KB |
testcase_19 | AC | 1,576 ms
85,208 KB |
testcase_20 | AC | 1,583 ms
85,220 KB |
testcase_21 | AC | 1,604 ms
84,928 KB |
testcase_22 | AC | 1,513 ms
83,984 KB |
testcase_23 | AC | 1,512 ms
83,120 KB |
testcase_24 | AC | 1,702 ms
85,904 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"))