September
InterviewTime limit1sMemory limit1024 MB
Given M recorded orders of leaf deletions in a rooted tree, find the largest K such that all records agree on which nodes fall on each of the K days.
- Level
Medium5 of 10
- Topics
- Tree, Array, Implementation
- Solved
- No attempts yet
Problem
The central square of Hangzhou has a famous ancient tree. It can be regarded as a rooted tree with nodes, indexed from to , where node is the root.
A node with no children is called a leaf node. Each time the ancient tree sheds leaves, it deletes one leaf node that exists at that moment. It may shed leaves several times on the same day.
There are volunteers, indexed from to , who guard the ancient tree. Each one independently records this year's leaf shedding with the following method.
Every day, collect the indices of all leaves that fell that day, and write them in any order after the records from earlier days.
For example, on the first day leaves and fall, so the record is or . On the second day leaves and fall, so the record continues with or . The final record can be any of , , , or .
The process lasts for days, with newly fallen leaves every day, until only the root node remains.
You are visiting Hangzhou in the middle of winter. Looking up at the bare branches of the ancient tree, you imagine the beautiful sight of falling leaves. You want to know how many days of falling leaves you could have seen this year, but you only have the records from the volunteers. From these records, find the maximum possible value of .
Constraints
- .
- .
- .
- . For every with , .
- For every with , the array is a permutation of .
- It is guaranteed that describes a rooted tree with node as the root.