Graph Theory37 sections · 1633 units
Open in Course

Problem - Planets Queries I

(CSES 1750)

You are given nn planets where each planet has a teleporter to another planet. Answer qq queries: starting from planet xx, where are you after kk steps? kk can be up to 10910^9. This is the direct application of binary lifting. Each planet is a node, and the teleporter defines f(x)f(x).

Simulating 10910^9 steps times out. I'll show you how to build the jump table and answer queries efficiently. The preprocessing takes O(nlogn)O(n \log n), but each query is O(logk)O(\log k).

Space complexity is O(nlogn)O(n \log n) for the data structures used.