Recently I read a problem on trees But I was finding difficulty in approaching this problem. Here is problem:
There is country with n cities (2 to 10^5) and (n-1) bidirectional roads, such that It is possible to travel between any pair of cities. We have 1 magic Truck which can travel between cities but It will take 1 unit of time (if it's loaded) and 0 unit time (if not loaded) in traveling between adjacent cities, and can hold at max 1 unit of product.
Now You can have a customer in any city who requires exactly 2 units of products and cannot wait more than 2 units of time.
Now question is, we have to minimise total number of storages with given restrictions:
Assign Storages in country so that you can fill atleast first order on time. Given that order can be placed in any city.
Time Limit: 1sec
What I have tried:
Worst approach I can think is Brute force. Try to place storage in every city(2^n possibilities) and check if every city order can be fulfilled with help of neighbouring cities. But Time Complexity of this will be (n*2^n). So will not work at all.
Second approach I am thinking is using DP on trees somehow. And I am also not sure If this will be optimal. From above problem I can ensure that leaves should have 1 storage for sure. And I was thinking DP like, start from root and check if children can help in fulfilling it's order and assign storage to that city accordingly, With base case on leaves. But problem Here is that children can also fulfil order from parent so it's making circular loops. So, It did not help me too.
Last approach I was thinking to apply binary search on answer itself. As answer can lie between(1,n) then answer may be found in nLog(n) order. But again problem is, I could not think an optimal way to assign storages in cities with given number of storages.
So, Thats it.. I was trying hard but could not solve this. Any help would be appreciated. :)
Note: I don't know why they make problem statement so complicated like this. They could easily explain problem in much simpler way. Resulting I cannot find this problem on web any more. It was somewhere on codeforces I guess.
Dynamic Programming(DP) is a technique to solve problems by breaking them down into overlapping sub-problems which follows the optimal substructure.
Dynamic tree data structures maintain forests that change over time through edge insertions and deletions. Besides maintaining connectivity information in logarithmic time, they can support aggregation of information over paths, trees, or both.
The standard All Pair Shortest Path algorithms like Floyd-Warshall and Bellman-Ford are typical examples of Dynamic Programming.
The important thing to note about the graph is that there are n cities and n-1 roads and all cities are reachable; this means that:
There are two possibilities for every city; either:
This also means that a singly-connected city (end-point of a road) always has a storage facility, and a string of doubly-connected cities should (optimally) alternatingly have a storage facility or not, which will give us a starting point when deciding where to put the storage facilities.
blue: current node; green: storage; orange: no storage; +: needs another neighbour with storage; ?: visited but not yet solved
That gives us the following algorithm:
This is basically a "travel down all roads and backtrack again" algorithm, so the number of visited nodes is 2×N and the complexity is linear or O(N).
It's worth noting that the order in which cities are visited will not change the result, i.e. the number of storage facilities, but it may change some of their locations. Consider these two solutions for 4 cities:
S - / - S - S (solved left to right)
S - S - / - S (solved right to left)
Moving closer to actual code, let's see what to do once you've identified an end-point. The graph consists of nodes like e.g. this:
NODE "city" C1
- neighbours: [C2, C4, C7]
- has_storage: undefined <- at first; will be set to true/false
- needs_more_storage: true/false <- we'll add this property as we go
- visited: true/false <- we'll add this property as we go
We start at an end-point, and then for each node, we'll:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With