Twibet (Small)
InterviewTime limit5sMemory limit512 MB
Starting from each monk in turn, count how many monks hear a whisper that spreads from a monk to all direct and indirect followers.
Problem
The holy country of Twibet has monks. Each monk carries a distinct number from to , and for religious reasons the monks use no names. They walk slowly around Twibet all the time, and each monk follows exactly one other monk.
Most days everyone stays silent. On day , monk stops, turns around and whispers the 140 Words of Wisdom. The whisper is quiet, so only the monks who follow him directly can hear it. Each monk who hears the words stops, turns around and whispers the same words to his own followers. The chain continues this way: every monk who has just heard the words and has not whispered them yet stops and whispers to his followers.
Once every monk who could hear the words has whispered them, they all turn back around and keep walking as usual. The next day the same thing happens, this time starting with a different monk.
For every between and , count how many monks whisper the 140 Words of Wisdom on day .
Input
The first line contains the number of test cases . test cases follow.
The first line of each test case contains an integer . The second line contains space separated integers . Monk follows monk .
Limits
- , (no monk follows himself)
Output
For each test case, output one line in the form Case #x:, where is the test case number starting from . Then output lines. The first line holds the number of monks who whisper the words on day 1, the next line the number on day 2, and so on through day .
Explanation
In the first test case of the example, the 3 monks walk in a single circle. Whoever whispers first, his follower whispers next and the remaining monk whispers after that, so all 3 monks whisper on each of the 3 days.
In the second test case, monk 1 follows monk 2, monk 2 follows monk 3, monk 3 follows monk 2, and monk 4 follows monk 1. On day 1 monk 1 whispers first and monk 4 hears him and whispers next, while monks 2 and 3 hear nothing that day. On day 2 monk 2 whispers first, monks 1 and 3 hear him and whisper, and finally monk 4 hears monk 1 and whispers last. On day 3 the monks whisper in the order 3, 2, 1, 4. On day 4 monk 4 whispers and nobody hears him.