U V W
Input: ‘N’ = 4, ‘ROADS’ = [[1, 2, 2], [1, 3, 3], [2, 4, 3]], ‘Q’ = 1, ‘Queries’ = [[3, 4, 2]]
Output: YES
If we remove the road between 2 and 4 and add the current road of queries then the sum becomes 7 which previously was 8.
The first line will contain the integer 'T', denoting the number of test cases.
The next line contains a single integer ‘N’ representing the number of villages.
The next ‘N’-1 lines contain three integers ‘U’, ‘V’, and ‘W’ representing the villages ‘U’ and ‘V’ where a road of distance ‘W’ is made.
The next line contains an integer ‘Q’ representing the number of queries.
The next ‘Q’ lines contain ‘U’, ‘V’, and ‘W’ representing the query of making a road between villages ‘U’, ‘V’ with a distance ‘W’.
For each test query, you should output ‘YES’ if the total distance strictly reduces else print ‘NO’.
You don't need to print anything. It has already been taken care of. Just implement the given function.
1 <= T <= 10
1 <= N <= 10^5
1 <= W <= 10^9
1 <= U, V <= N
1 <= Q <= 10^5
Time Limit: 1 sec
Now if you read the problem carefully then if you find out the connection is nothing but a tree as all the villages are connected so to connect all nodes we need at least ‘N’-1 roads and the connected graph with ‘N’ nodes and ‘N’-1 roads is nothing but a tree.
Now in a query, we add one road between two villages and remove one road from the tree in such a way that the graph still remains connected means the graph still remains a tree.
In the naive approach for each query, We will be provided with two villages let’s say those are ‘u’ and ‘v’ and we have to find out the maximum distance between two adjacent villages which are directly connected by a road with each other and they are on the path when we are going from village ‘v’ to village ‘u’ or vice versa and then we check if the maximum distance we found earlier is greater than the value we have provided with the new road in query than the answer is ‘YES’ else answer is ‘NO’ push appropriate answer in result ‘ans’ and return that array.
The following uses the concept of binary lifting and the lowest common ancestor more about it you can read the Binary Lifting Technique. Please read that article first if you do not know how binary lifting works so first learn that technique.
Now if you read the problem carefully then if you find out the connection is nothing but a tree as all the villages are connected so to connect all nodes we need at least ‘N’-1 roads and the connected graph with ‘N’ nodes and ‘N’-1 roads is nothing but a tree.
Now in a query, we find out we add one road between two villages and remove one road from the tree in such a way that the graph still remains connected means the graph still remains a tree.
Now we want to add one road between villages ‘U’ and ‘V’ so we will remove any road which comes on the path of ‘U’ to ‘V’. Now why is that because we add one road and remove one road so total roads still are ‘N’ - 1 and if we remove some road other than this path’s road then that part of the tree will get disconnected and the ‘U’ and ‘V’ villages will have a cycle one path was previously from ‘U’ to ‘V’ we added one more from the ‘U’ to ‘V’ both of these cases violates the property of the tree. You can learn more about it by drawing the tree and adding one road and removing various roads from it.
Now we figured out this much, then we will create a 2-D array that will store its ancestor of it. We will store the ancestor in the following way 1, 2, 4, 8…… we will store another array ‘maxValue’ which will store the longest distance road we encountered while reaching the ‘ith ancestor where is [1, 2, 4….].
Now for each query, we will check the largest distance road we encountered on the road from villages to the there lowest common ancestor. If that road is greater than the new road we are adding then the answer is ‘YES’ else answer is ‘NO’.