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 | A | B | A | (| A | means size of A ) | ⋃ B = | + | − | ⋂ B Let num i 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, head i , where head i = 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 To effectively calculate the size of the um = um um A | n head u : n head u + n head v − | u ⋂ A v intersection, we can keep the array B where B v is the intersection value of the edge (u, v). Thus, the transition becomes um = um um n head u : n head u + n head v − B v When deleting an edge (u,v) where u has a lower depth, we simply do to properly update the new sizes of the components (since the actual um = um n head v : n head u size is only stored in ). Notice that B i will only need to be updated when the edge is um n head u removed. Therefore, we need to do um B i = n head u Time complexity: ( n log n ) O 2 random solutions for your reference: https://ideone.com/SIc4W3 (syy) https://ideone.com/RQtWLc (jiahng) (apparently less readable)