Starting from each monk in turn, count how many monks hear a whisper that spreads from a monk to all direct and indirect followers.
Easy3GraphDFSInterviewNo attempts yetTime limit5sMemory limit512 MBThe holy country of Twibet has N monks. Each monk carries a distinct number from 1 to N, 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 K, monk K 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 K between 1 and N, count how many monks whisper the 140 Words of Wisdom on day K.
The first line contains the number of test cases T. T test cases follow.
The first line of each test case contains an integer N. The second line contains N space separated integers F1,F2,…,FN. Monk i follows monk Fi.
For each test case, output one line in the form Case #x:, where x is the test case number starting from 1. Then output N 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 N.
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.