NewProject Coding is here — build features and fix bugs in real codebases with an AI assistantSee what's new

You are given a directed tree with n nodes labeled from 1 to n. The tree contains n - 1 directed edges, where the ithi^{\text{th}} edge points from from[i] to to[i].

You may choose any node as the root. After choosing the root, you may reverse some edges so that every edge points away from the root.

Return the minimum number of edge reversals needed over all possible choices of the root.

Constraints:

  • 1 ≤ n ≤ 10510^5
  • 1 ≤ from[i], to[i] ≤ n
  • There is at most one edge between any two nodes
Test Cases
Input
Output
Expected