Trick or Treat on the Farm
Time limit1sMemory limit128 MB
Each stall has one outgoing next pointer. For every starting stall, count how many distinct stalls the walk visits before it first revisits a stall.
- Level
Medium6 of 10
- Topics
- Graph, DFS, Implementation, Array
- Solved
- No attempts yet
Problem
Every year in Wisconsin the cows celebrate the American autumn holiday of Halloween by dressing up in costumes and collecting the candy that Farmer John leaves in the () stalls, conveniently numbered .
Because the barn is not very large, FJ makes the fun last longer by specifying a traversal route the cows must follow. To do this he posts on each stall a "next stall number" () that tells the cows which stall to visit next; the cows may therefore travel back and forth through the barn many times while collecting candy.
FJ requires that cow start collecting candy at stall . A cow stops collecting the moment she arrives at any stall she has already visited.
Compute the number of distinct stalls each cow visits before she is forced to stop.
Input
- Line 1: a single integer .
- Lines 2 to : line contains a single integer .
Output
- Lines 1 to : line contains a single integer, the total number of distinct stalls visited by cow before she returns to a stall she has already visited.