Олимпиадный тренинг

Задача 39575. Dance moves


Farmer John's cows are dancing.
First, all N cows (2≤N≤105) are in a row and cow i is in position i. The sequence of dance movements is given by K (1≤K≤2⋅105) pairs of positions (a1,b1),(a2,b2),…,(aK,bK). Every minute i=1…K of the dance, the cows in positions ai and bi change places. The same K exchanges are made on minutes K+1*2K, then again on minutes 2K+1*3K, and so on. In other words,

at minute 1 the cows at positions a1 and b1 are swapped.
at minute 2, the cows at positions a2 and b2 are swapped.
...
At minute K, the cows at positions aK and bK swap.
At minute K+1, the cows at positions a1 and b1 are swapped.
At minute K+1, the cows at positions a2 and b2 are swapped.
etc. ...
For each cow, determine the number of unique positions it will visit during the endless dance.

Input
The first line of input contains the integers N and K. Each of the subsequent K lines contains (a1,b1)…(aK, bK) (1≤ai<bi≤N).
Imprint
Print N lines, where the i-th line contains the number of unique positions that cow i will visit.
Examples
# Input Output Explanation
1
5 4
13
12
2 3
24
4
4
3
4
1
  • Cow 1 will reach positions {1,2,3,4}.
  • Cow 2 will reach positions {1,2,3,4}.
  • Cow 3 will reach positions {1,2,3}.
  • Cow 4 will reach positions {1,2,3,4}.
  • Cow 5 will not move and will not leave position 5.