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"))