← Back to companies
[ OK ] Loaded —
[ INFO ]
$ cd
$ ls -lt
01
02
03
04
05
$ ls -lt
01
02
03
04
05
user@intervues:~/$
You are given a directed graph with gNodes nodes numbered from 1 to gNodes and gNodes - 1 directed edges. The ith edge goes from gFrom[i] to gTo[i].
The underlying undirected graph is guaranteed to form a tree.
Choose any node as the root. You may reverse directed edges so that after all reversals, every edge points away from the chosen root.
Return the minimum number of edge reversals needed over all possible root choices.
Choosing node 2 as the root works best. Reverse edges 1 -> 4 and 3 -> 4 so every edge points away from node 2.
If node 1 is the root, every edge already points away from it, so no reversal is needed.