This page is still under construction.

Parts of this page are still being built. What you see may change.

Tree Allocation

Time limit10sMemory limit64 MB

Summary
Partition the nodes into blocks of at most B to minimize the worst root-to-leaf block count, for every choice of root.
Level

Hard8 of 10

Topics
Dynamic programming, Tree, Greedy
Solved
No attempts yet

Problem

A tree is one of the most widely used data structures in computer science. The structure itself is simple, but it does not fit the way a computer stores data. Memory behaves like a one dimensional array, and a tree is not one dimensional, so cache efficiency becomes a problem once the tree is placed in memory. Data that sits in the cache is read faster, so consider an allocation with good cache behaviour.

The cost of an allocation is defined as follows. Memory is split into blocks, and one block stores at most BB nodes of the tree. Reading data in a block brings the whole block into the cache, and every piece of data in that block is then read faster. The cost of reading node uu right after node vv is 00 when uu and vv are in the same block, and 11 otherwise. The cache starts empty, so the first node read on a path always costs 11. The cost of the path v1,v2,…,vnv_1, v_2, \dots, v_n is the sum of the costs of reading those nodes in that order. The cost of an allocation of a tree is the maximum cost over the paths that run from the root to each terminal node. A terminal node is a node with no child under the chosen root, and in a tree with a single node the root itself is the terminal node.

The figure below shows two allocations of a tree with 10 nodes when B=4B = 4 and node 1 is the root. Each frame is one block. The allocation on the left costs 33 and is not optimal. The allocation on the right reaches the optimal cost 22.

Example of two block allocations

Given a tree, compute the minimum cost of allocation when node ii is the root, for every node ii.

Input

The input contains several test cases. The first line of each test case has two integers NN and BB separated by a single space (1≤N≤1000001 \le N \le 100000, 1≤B≤N1 \le B \le N). Each of the next N−1N - 1 lines contains one integer. The ii-th integer pip_i (1≤pi≤i1 \le p_i \le i) means that node i+1i + 1 and node pip_i are connected. The nodes are numbered from 11 to NN.

The last test case is followed by a line containing two zeros. That line is not a test case.

Output

For each test case, print Case x: on its own line, where xx is the test case number starting from 11. Then print NN lines. The ii-th line contains the minimum cost of allocation of the given tree when node ii is the root.

Examples2

  1. Example 1

    Input
    3 1
    1
    2
    3 2
    1
    1
    10 4
    1
    1
    2
    3
    3
    4
    4
    4
    5
    0 0
    
    Expected output
    Case 1:
    3
    2
    3
    Case 2:
    2
    2
    2
    Case 3:
    2
    2
    2
    2
    2
    2
    2
    2
    2
    3
    
  2. Example 2

    Input
    1 1
    2 1
    1
    2 2
    1
    3 3
    1
    1
    4 4
    1
    2
    3
    0 0
    
    Expected output
    Case 1:
    1
    Case 2:
    2
    2
    Case 3:
    1
    1
    Case 4:
    1
    1
    1
    Case 5:
    1
    1
    1
    1