You are given a graph and asked if there is any node u such that:
A DFS is enough for the latter.
For the former, you have to check if there exists some cycle like that. Notice that $ \log (x) = (-ve) $
where $ 0 < x < 1 $
. Lets convert the edge weights to their log values in any suited base. Since $ \log (UV) = \log U + \log V $
, our sub-problem is now reduced to finding a cycle with negative sum. This can be done with Bellman–Ford algorithm under the given constraints.
So our solution skeleton would be like this:
Should you need it, find my C++ implementation here: https://gist.github.com/rebornplusplus/3b8d22de1e8a1b7f4419ef053c8e0500.
P.S: The initial reading d doesn't do much. I thought you could use a joke. 😛