## Editorial for Wesley's Anger Contest 4 Problem 7 - Squirrel Cities

**only**when stuck, and

**not to copy-paste code from it**. Please be respectful to the problem author and editorialist.

**Submitting an official solution before solving the problem yourself is a bannable offence.**

Author:

You may recognize similarities with the problem Enchanted Forest from the 2014 Chinese National Olympiad in Informatics.

##### Subtask 1

For the first subtask, we can construct a graph for each query using only edges with and , and determining if the graph is connected. This can be done in many ways such as breadth first search, depth first search, or the Union-Find data structure.

**Time Complexity:** or

##### Subtask 2

For the second subtask, we can make the observation that for each unique value of , there will be a minimum value of where the graph is completely connected. In fact, as decreases, the minimum value of decreases. In order to find this minimum value of for each , we need to find a minimum bottleneck spanning tree, which can be done by constructing a minimum spanning tree with all edges with a durability of at least , where the weight of each edge is . The minimum value of is equal to the maximum value of in the minimum spanning tree (assuming a tree exists). If the edges are presorted by , this can be done in time using Kruskal's algorithm. To answer the queries, we can binary search for the smallest value where and use the precomputed answer for that value. Alternatively, a two pointer approach can be used the answer the queries offline.

**Time Complexity:**

##### Subtask 3

When edges are being added to the graph in decreasing order of , we can maintain the minimum spanning tree efficiently. When an edge is added to the previous minimum spanning tree, it will create some cycle. If the edge with the maximum edge weight is deleted from this cycle, then we will obtain the new minimum spanning tree. We can find this edge using a Link-Cut Tree with nodes where the value of the first nodes are and the value of the node is representing the edge (we can imagine this as a node in the middle of an edge). We can query for the maximum edge using this data structure, while also maintaining a set of the edge weights in the minimum spanning tree in order to answer queries in a similar manner as subtask .

**Time Complexity:**

There exists other solutions such as square root decomposition, and divide and conquer.

## Comments