Flight Code Reappearance Map
Count equal-label paths by their highest vertex without repeats
An airline stores a branching flight schedule as a rooted tree. Each vertex represents a flight, and its parent is the flight from which its schedule branch continues. Flight 0 is the root.
You receive arrays parent and codes. For every vertex i > 0, parent[i] is its parent, and parent[i] < i. The value parent[0] is -1. The integer codes[i] is the operating code of flight i.
A pair of distinct vertices is a clean reappearance when:
- Its two endpoints have the same operating code.
- No vertex strictly between the endpoints on their unique tree path has that code.
Other codes may appear any number of times on the path. In particular, adjacent vertices with equal codes form a clean reappearance.
The highest vertex of a path is its vertex closest to the root, equivalently the lowest common ancestor of its endpoints.
Return an integer array answer of length n, where answer[u] is the number of clean reappearances whose highest vertex is u. Count each unordered endpoint pair once.
Examples
Example 1
Input: parent = [-1,0,0,1], codes = [7,4,4,7] Output: [2,0,0,0]
Vertices 1 and 2 have the same code, and their path passes through vertex 0, whose code differs. Their pair is attributed to vertex 0. Vertices 0 and 3 also form a clean reappearance attributed to vertex 0.
Example 2
Input: parent = [-1,0,0,1,1], codes = [3,3,3,3,3] Output: [2,2,0,0,0]
Every flight has the same code. Only adjacent endpoints qualify, because any longer path contains an interior vertex with that code. Each qualifying pair is attributed to its parent endpoint.
Example 3
Input: parent = [-1,0,1,1,2,4], codes = [8,5,2,2,8,8] Output: [1,1,0,0,1,0]
Vertices 2 and 3 form a clean reappearance whose highest vertex is 1. Vertices 0 and 4 also qualify, but vertices 0 and 5 do not: vertex 4 repeats their code inside that path.
Constraints
- 1 <= n <= 100000
- parent.length == codes.length == n
- parent[0] == -1
- 0 <= parent[i] < i for every 1 <= i < n
- 0 <= codes[i] <= 1000000000
- Every answer entry fits in a signed 64-bit integer
The intended solution uses small-to-large map merging in expected O(n log n) time and O(n) auxiliary space. Use an iterative processing order to support long chains. JavaScript Number represents all possible counts exactly under these constraints.
Hints
Show hint 1Hint 1
For a subtree, track how many endpoints of each code can be reached from its root without passing an earlier vertex with that same code.
Show hint 2Hint 2
Combine contributions from different child subtrees, treating the current vertex's code separately. Reuse the largest child's map so that entries from smaller maps are moved only a logarithmic number of times.
Follow-up questions
What an interviewer might ask once you have a working solution.
- How would you return only the total number of clean reappearances while reducing unnecessary output storage?
- How would you adapt the implementation if vertex indices were not ordered before their descendants?
Practice this with an AI interviewer
Explain your approach out loud, write Python or JavaScript, run it against hidden tests (including large inputs), and get a scored debrief.
Start this problem