You’re building a mobile file manager for a fintech app used by 10M+ monthly active users. Users can drag items in a hierarchical file tree to reorganize statements, tax documents, and receipts. A single bad move (like dropping a folder into its own descendant) can corrupt the tree and break compliance exports.
You are given a rooted tree of n nodes (root is node 0). Each node is either a folder or a file. The tree is represented by:
parent[i]: the current parent of node i (parent[0] = -1)is_folder[i]: True if node i is a folder, else FalseYou also receive a list of drag-and-drop operations ops, where each operation is a pair (src, dst) meaning: move node src to become a direct child of node dst.
Apply operations in order. An operation is ignored if any of the following holds:
src == 0 (the root cannot be moved)src == dstis_folder[dst] == False (you can only drop onto folders)dst is in the subtree of src at the time of the operation)If the move is valid, update parent[src] = dst.
Return the final parent array.
Example 1
parent = [-1, 0, 0, 1, 1], is_folder = [True, True, True, False, False], ops = [(3, 2)][-1, 0, 0, 2, 1]3 is a file currently under folder 1. Folder 2 is a valid destination and is not inside 3’s subtree, so the move succeeds.Example 2
parent = [-1, 0, 1, 2], is_folder = [True, True, True, True], ops = [(1, 3)][-1, 0, 1, 2]3 is a descendant of 1. Moving 1 under 3 would create a cycle, so the operation is ignored.parent always forms a valid rooted tree.parent array; child ordering is irrelevant.1 <= n <= 2 * 10^5len(ops) <= 2 * 10^5parent[0] = -10 <= src, dst < n