Synchronisation Abridged statement: Given a tree with N nodes, process Q operations for activating/deactivating an edge. Each node starts with a unique piece of information. When activating an edge, information can be transmitted across that edge. Find the final pieces of information of K nodes. Solution Important fact: |A ⋃ B | = |A| + |B| − |A ⋂ B | (|A| means size of A) Let numi be the number of distinct pieces of information node i has. Arbitrarily root the tree. Notice that at every point of time, each node in the same connected component contains the same information. Thus, at each point of time, we can assign each node i to a head, headi, where headi = node with the lowest depth connected to (i.e. furthest ancestor). We will use a modified HLD to keep track of the head of each node at any point in time. For every chain of nodes, we store a set of the edges that are currently unavailable. For a node, we try to traverse from its current chain to the chain above it. This can be done by just checking if the first broken edge (sorted by increasing depth) is above the current node. If the path is unobstructed, we jump up to the next chain. We keep on doing this until we are unable to jump up further. Then we can binary search once to find the head of the query node. Since there are at most log n chains, the complexity to find the head of any node is O(log n). When adding an edge (u,v) where u has a lower depth, we do num head u := num head u + num head v − |A u ⋂ A v | . To effectively calculate the size of the intersection, we can keep the array B where Bv is the intersection value of the edge (u, v). Thus, the transition becomes num head u := num head u + num head v − B v . When deleting an edge (u,v) where u has a lower depth, we simply do num head v := num head u to properly update the new sizes of the components (since the actual size is only stored in num head u ). Notice that Bi will only need to be updated when the edge is removed. Therefore, we need to do B i = num head u . Time complexity: O(n log n) 2 random solutions for your reference: https://ideone.com/SIc4W3 (syy) https://ideone.com/RQtWLc (jiahng) (apparently less readable)
Enter the password to open this PDF file:
-
-
-
-
-
-
-
-
-
-
-
-