Datasets:

Modalities:
Image
Text
Formats:
parquet
Size:
< 1K
Tags:
code
Libraries:
Datasets
pandas
License:
File size: 3,575 Bytes
761e24d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<p>
The fine people of Corpro Corp. are a festive bunch. Every holiday season, everybody buys a gift for their manager. A cynic might say that the employees are just trying to bribe their way to a better performance review, but if you asked them yourself, they'd say they just wanted to spread cheer.
</p>

<p>
The fine people of Corpro Corp. are a frugal bunch. When they buy gifts, they cooperate to collectively buy the least expensive gifts that they can. A cynic might say that the employees are cheap, but if you asked them yourself, they'd say it's the thought that counts.
</p>

<p>
There are <strong>N</strong> employees working at Corpro Corp., and each of them has a manager, except for the CEO who has no manager (the CEO also buys a gift every year, but she donates it to charity). 
The employees each have a unique employee ID which is an integer from 1 to <strong>N</strong>. As you might expect, the CEO has the ID 1.
</p>

<p>
If there exists a set of two or more employees
{<strong>p<sub>1</sub></strong>, ..., 
<strong>p<sub>k</sub></strong>}
such that, for all <strong>i</strong> &lt; <strong>k</strong>,
<strong>p<sub>i</sub></strong> is the manager of <strong>p<sub>i+1</sub></strong>,
then we say that <strong>p<sub>1</sub></strong> is "responsible for" <strong>p<sub>k</sub></strong>.
There are never two employees who are responsible for each other.
That would be a silly hierarchy indeed.
</p>

<p>
There are <strong>N</strong> kinds of gifts available for purchase, and the <strong>i</strong>th kind of gift costs <strong>i</strong> dollars. That is, the prices of the different kinds of gifts are {$1, $2, $3, ... $<strong>N</strong>}. There are <strong>N</strong> copies of each gift available for purchase.
</p>

<p>
The only thing that stops all employees from purchasing gifts that cost $1 is the awkwardness of buying a gift for their manager that's the same as the one their manager is giving away. No employee would ever do such a thing! 
</p>

<p>
For example, in a company with just 2 employees, at least $3 must be spent in total. If employee #1 (the CEO) buys a $1 gift to donate to charity, then employee #2 cannot buy a $1 gift for employee #1 (their manager), but they can buy a $2 gift instead. Note that it would be equally optimal for the CEO to buy a $2 gift, while receiving a $1 gift from her subordinate.
</p>

<p>
What's the minimum possible total expenditure across the whole company during the gift exchange?
</p>


<h3>Input</h3>

<p>
Input begins with an integer <strong>T</strong>, the number of corporate hierarchies to consider. 
Each hierarchy is made up of two lines.
The first line contains the integer <strong>N</strong>.
The second line contains <strong>N</strong> space-separated integers.
The <strong>i</strong>th integer is the employee ID of the manager of employee <strong>i</strong>,
with the exception that the first integer is always 0, denoting that the CEO has no manager.
</p>



<h3>Output</h3>

<p>
For the <strong>i</strong>th hierarchy, print a line containing "Case #<strong>i</strong>: " followed by the smallest amount of money the entire company would need to spend.
</p>

<h3>Constraints</h3>

<p>
1 &le; <strong>T</strong> &le; 100 <br />
1 &le; <strong>N</strong> &le; 200,000 <br />
</p>

<p>
<strong>NOTE:</strong> The input file is about 10-20MB.
</p>

<h3>Explanation of Sample</h3>
<p>
In the first test case, the CEO will spend $2, and the other employees will spend $1.
</p>

<p>
In the second test case, employees #2 and #3 will spend $2, and the other employees will spend $1.
</p>