Skip to content

CF1370F2 The Hidden Pair

Step 1

First we can check for all. $\(\text{ans} = \min \iff S \text{ contains an element in the path from u to v}\)$ We are essentially trying to find the path's endpoints. But we also know the tree's structure. We can root the tree at the first returned point \(w\), and ask the maximum depth. But no, we might get \(u \rightarrow p \rightarrow \max \rightarrow q \rightarrow v\). We'll have to binary search to deeper of \(u\) and \(v\). Since it's deeper, the depth range we have to search is \(\left[\left\lceil \frac{d}{2} \right\rceil, d\right]\).

Once we find one of them, we can reroot at \(u\) and query \(d\). Complexity: \(\mathcal{O}(\log_2{n} + 1)\).