Under construction

Still being built. Things may change

September

Interview

Time limit1sMemory limit1024 MB

Summary
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 NN nodes, indexed from 00 to N−1N - 1, where node 00 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 MM volunteers, indexed from 00 to M−1M - 1, 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 33 and 44 fall, so the record is 3,43, 4 or 4,34, 3. On the second day leaves 11 and 22 fall, so the record continues with 1,21, 2 or 2,12, 1. The final record can be any of (3,4,1,2)(3, 4, 1, 2), (4,3,1,2)(4, 3, 1, 2), (3,4,2,1)(3, 4, 2, 1), or (4,3,2,1)(4, 3, 2, 1).

The process lasts for KK 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 MM volunteers. From these records, find the maximum possible value of KK.

Constraints

  • 2≤N≤1052 ≤ N ≤ 10^5.
  • 1≤M≤51 ≤ M ≤ 5.
  • ∑NM≤8×105\sum NM ≤ 8 \times 10^5.
  • F[0]=−1F[0] = -1. For every ii with 1≤i≤N−11 ≤ i ≤ N - 1, 0≤F[i]≤i−10 ≤ F[i] ≤ i - 1.
  • For every ii with 1≤i≤M−11 ≤ i ≤ M - 1, the array S[i]S[i] is a permutation of 1,2,…,N−11, 2, \dots, N - 1.
  • It is guaranteed that FF describes a rooted tree with node 00 as the root.

Examples1

  1. Example 1

    Input
    2 1
    -1 0
    1
    
    Expected output
    1