Datasets:

Modalities:
Image
Text
Formats:
parquet
Size:
< 1K
Tags:
code
Libraries:
Datasets
pandas
License:
hackercup / 2019 /round3 /chain_of_command.html
wjomlex's picture
2019 Problems
e329daf verified
raw
history blame
6 kB
<p>
At last, Mr. X has managed to return to the school at which he formerly taught, with the intention of confronting his suspicious successor, Mr. Y.
However, it appears that Mr. Y has fortified his position &mdash; he has brainwashed all of his students into believing that he's the world's best teacher
through the timeless ploy of replacing classes with extra recesses. There's no way that Mr. Y can be thrown out of the school without first compromising his students' support!
</p>
<p>
There are <strong>N</strong> students in Mr. Y's class, with IDs from 1 to <strong>N</strong>. In an attempt to maximize security, Mr. Y has arranged them into a hierarchical structure,
with each student <em>i</em> either reporting to a commanding student <strong>C<sub>i</sub></strong>, or not reporting to any other student
(indicated by <strong>C<sub>i</sub></strong> = 0). There's exactly one student <em>i</em> for whom <strong>C<sub>i</sub></strong> = 0, who is the class leader.
</p>
<p>
A <em>chain of command</em> is a sequence of one or more students going up the hierarchical structure starting from some student <em>i</em> and ending somewhere between
<em>i</em> and the class leader (inclusive): <em>i</em> &rarr; <strong>C<sub>i</sub></strong> &rarr; <strong>C<sub>C<sub>i<sub></sub></strong> &rarr; etc.
It's guaranteed that, for each student <em>i</em>, there exists a <em>chain of command</em> beginning at them and ending at the class leader.
</p>
<p>
Initially, all <strong>N</strong> students are under Mr. Y's control. However, Mr. X is about to perform some bribery of his own. One by one, in order from 1 to <strong>N</strong>,
Mr. X will bribe each student with healthy snacks. After the first <em>b</em> bribes, students 1..<em>b</em> will be under Mr. X's control instead of Mr. Y's.
</p>
<p>
After each of the <strong>N</strong> bribes, Mr. X would like to evaluate the vulnerability of Mr. Y's class to a potential takeover.
To do so, he'll determine <strong>N</strong> hypothetical values:
for each student <em>i</em>, he'll compute the maximum length that a <em>controlled chain</em> beginning with student <em>i</em> could possibly have if 0 or more <em>promotions</em>
were to first take place (or 0 if no such chain could exist).
</p>
<p>
A <em>controlled chain</em> is a <em>chain of command</em> consisting exclusively of students under Mr. X's control. A <em>promotion</em> is a modification to the class structure
in which Mr. X selects a certain student <em>j</em> with a commanding student <em>c</em> = <strong>C<sub>j</sub></strong>
(such that <em>c</em> &ne; 0 and <em>c</em> &ne; <em>i</em>), expels student <em>c</em> (removing them from the class entirely), and brings <em>j</em>
up to occupy <em>c</em>'s former place (setting <strong>C<sub>j</sub></strong> to <strong>C<sub>c</sub></strong>,
and for each other student <em>k</em> such that <strong>C<sub>k</sub></strong> = <em>c</em>, now setting <strong>C<sub>k</sub></strong> to j).
Note that each of these hypothetical values should be considered independently of the others; Mr. X will never actually perform any <em>promotions</em> and permanently alter the class structure.
</p>
<p>
In order to reduce the size of the output, these <strong>N<sup>2</sup></strong> values should be aggregated into a single integer as follows:
Letting <strong>S<sub>b</sub></strong> be the sum of the <strong>N</strong> students' maximum <em>controlled chain</em> lengths after the first <em>b</em> students have been bribed,
output (<strong>S<sub>1</sub></strong> * <strong>S<sub>2</sub></strong> * ... * <strong>S<sub>N</sub></strong>) modulo 1,000,000,007.
</p>
<h3>Input</h3>
<p>
Input begins with an integer <strong>T</strong>, the number of times Mr. X needs to wrest control from Mr. Y.
For each time, there is first a line containing the integer <strong>N</strong>.
Then, <strong>N</strong> lines follow, the <em>i</em>th of which contains the integer <strong>C<sub>i</sub></strong>.
</p>
<h3>Output</h3>
<p>
For the <em>i</em>th time, print a line containing "Case #<em>i</em>: "
followed by a single integer, the product of all <strong>N</strong> sums of maximum <em>controlled chain</em> lengths, modulo 1,000,000,007.
</p>
<h3>Constraints</h3>
<p>
1 &le; <strong>T</strong> &le; 80 <br />
1 &le; <strong>N</strong> &le; 800,000 <br />
0 &le; <strong>C<sub>i</sub></strong> &le; <strong>N</strong> <br />
</p>
<h3>Explanation of Sample</h3>
<p>
In the first case, after the first student has been bribed, a length-1 <em>controlled chain</em> can begin at student 1, while no <em>controlled chain</em> can begin at student 2.
Once the second student has also been bribed, a length-1 <em>controlled chain</em> can still begin at student 1, while a length-2 <em>controlled chain</em>
may now begin at student 2 (2 &rarr; 1). This results in a total answer of (1 + 0) * (1 + 2) = 3 (modulo 1,000,000,007).
</p>
<p>
In the second case, after the first two students have been bribed, a length-2 <em>controlled chain</em> can begin at student 1 (1 &rarr; 2) if student 1 gets promoted (replacing student 3).
The total answer comes out to (1 + 0 + 0) * (2 + 1 + 0) * (3 + 1 + 2) = 18 (modulo 1,000,000,007).
</p>
<p>
In the third case, after the first two students have been bribed, a length-2 <em>controlled chain</em> may begin at student 1 (1 &rarr; 2) if student 2 gets promoted (replacing student 3),
and a length-1 <em>controlled chain</em> can similarly begin at student 2 (2 &rarr; 1) if student 1 gets promoted (replacing student 3).
The total answer comes out to (1 + 0 + 0) * (2 + 2 + 0) * (2 + 2 + 1) = 20 (modulo 1,000,000,007).
</em>
<p>
In the fourth case, the total answer is (1 + 0 + 0 + 0 + 0) * (2 + 2 + 0 + 0 + 0) * (2 + 3 + 2 + 0 + 0) * (2 + 3 + 2 + 1 + 0) * (2 + 3 + 2 + 1 + 3) = 2464 (modulo 1,000,000,007).
</p>
<p>
In the fifth case, the total answer is 1 * 3 * 7 * 11 * 14 * 16 * 20 * 28 * 33 * 39 * 44 * 47 = 790446393 (modulo 1,000,000,007).
</p>