Dance Mooves
Time limit1sMemory limit512 MB
Given K position swaps repeated forever, count for each cow how many distinct positions in the line she ever occupies.
- Level
Hard8 of 10
- Topics
- Graph, Simulation, Hash map, Implementation
- Solved
- No attempts yet
Problem
Farmer John's cows are showing off their new dance mooves!
At first, all cows () stand in a line with cow in the th position in line. The sequence of dance mooves is given by () pairs of positions . In each minute of the dance, the cows in positions and in line swap. The same swaps happen again in minutes , again in minutes , and so on, continuing indefinitely in a cyclic fashion. In other words,
- In minute , the cows at positions and swap.
- In minute , the cows at positions and swap.
- ...
- In minute , the cows in positions and swap.
- In minute , the cows in positions and swap.
- In minute , the cows in positions and swap.
- and so on ...
For each cow, please determine the number of unique positions in the line she will ever occupy.
Input
The first line contains integers and . Each of the next lines contains ().
Output
Print lines of output, where the th line contains the number of unique positions that cow reaches.