Editorial We denote the maximum number of edges in the graph by $$$M$$$. You can see…
- User
Editorial
We denote the maximum number of edges in the graph by . You can see that is an upper bound for it. Subtask 2 We can store each operation, and replay them for each question. We can store all edges in a data structure, or keep neighbours in separate data structures for each node. When answering a question, find the neighbours of both nodes, and consider the distance of each pair of neighbours, computing the minimum. This yields to a simple solution in time, or when using an associative container, like std::set. Ordering Trick Although not required for this subtask, we can improve the second part by ordering the neighbour lists by value, and stepping through them simultaneously, using two pointers. We always step in the list in which the pointer points to the entry with the smaller value out of the two, and consider the current pair for the minimum computation: The correctness of this is easy to prove. Subtask 3 for all queries In this case, each question will refer to the same version (the final one). Hence, we can just apply all updates at the start once (applying updates is done in the same way as in Subtask 2), and then answer questions on this single version (like before). Using an efficient data structure and the ordering trick (we can actually pre-sort, before answering any questions), we can solve this subtask in . Subtask 4 for all nodes For a node and version , we need to be able to tell whether had a neighbour with for both possible values of ( or ). Once we have obtained this information for both and , we can easily work out the answer. For each node and possible value, let us build an ordered list of 'events' of the following types: • Node stopped having any neighbours with in version . • Node started having neighbours with in version . This yields a solution in time and space for this special case. Subtask 5 checkpoints We first apply all updates in order, producing checkpoints, evenly spaced, then – for each question – we simulate updates from the closest checkpoint (in the same way as we did for Subtask 1). An efficient implementation of this can achieve time and memory. Save neighbour list by node, binary search by version Another solution is to separate updates by node, and save the neighbour list of the updated node for each update (in a vector of neighbour lists for that node). Then, we can binary search for the neighbour list at version . This can be implemented in time and space. Self-copying data structures We can use a self-copying (also known as persistent or copy-on-write) data structure. These data structures are constructed as a (directed) tree, where each node holds some information. When updates are applied, we copy every node that was modified, including nodes whose children are modified, thus each version will have its own root node, from which queries can be performed. There are multiple possibilities here from static binary trees (holding neighbour lists, or neighbours directly) to balanced binary search trees (e.g. treap). These will solve this subtask, but will struggle to gain full marks for the problem due to exceeding memory constraints: the static versions have a high complexity ( space, or worse), and the BST version has very high constants (both in space and time). One segment tree We need to find when each edge is present in the graph. For each edge, this is the union of contiguous intervals. In total, we have at most intervals. Take a segment tree of length , with one leaf per version. Each node will contain a vector of edges, ordered by starting node (edge is stored in the tree node for , if edge is present in each version during the interval – and not entirely present in the parent's interval). This has a combined space requirement of . For each question, we look up edges starting from and using binary search in the vector of each of the relevant tree nodes. This can be implemented in time and space. With a few tricks and an efficient implementation, this solution could possibly pass subtask 6. Subtask 6 No additional constraints Save neighbour list by node, reduce storage space by constant factor Clearly, saving each version of the neighbour lists of each node will not fit in memory for these limits. However, since memory limits are very generous, we can cut this down by a reasonably small constant : we only save the neighbour list of node after every 'th update that affects . We also save an ordered list of updates affecting each node. For each question, we only need to replay at most updates ( for and for ). Choosing will suffice the pass every subtask. The time complexity is , using memory. We think this solution is interesting in the sense that it demonstrates how big- complexity can often be misleading for real-world problems: this solution is far simpler than other solutions, some of which do not even gain marks, and it passes the limits because the constants involved (normally hidden by big- complexity classes) are far smaller. Multiple segment trees We can eliminate the binary search from the previous segment tree solution, by keeping a separate segment tree for each node. Na"ively, this will not fit in the memory limits. However, we can save space by only keeping a leaf for each update where the given node was affected. This yields a solution in time and space.
Assistant