#P7299. [USACO21JAN] Dance Mooves S

[USACO21JAN] Dance Mooves S

Description

Farmer John’s cows are showing off their latest dance moves.

At the beginning, all NN cows (2N1052 \le N \le 10^5) stand in a line, and cow ii is in the ii-th position. The dance move sequence is given as KK (1K2×1051 \le K \le 2 \times 10^5) pairs of positions (a1,b1),(a2,b2),,(aK,bK)(a_1,b_1),(a_2,b_2),\ldots,(a_K,b_K). In minute i=1Ki=1 \ldots K of the dance, the cows at positions aia_i and bib_i swap places. The same KK swaps happen again in minutes K+12KK+1 \ldots 2K, again in minutes 2K+13K2K+1 \ldots 3K, and so on, repeating forever. In other words,

  • In minute 11, the cows at positions a1a_1 and b1b_1 swap places.
  • In minute 22, the cows at positions a2a_2 and b2b_2 swap places.
  • \ldots
  • In minute KK, the cows at positions aKa_K and bKb_K swap places.
  • In minute K+1K+1, the cows at positions a1a_1 and b1b_1 swap places.
  • In minute K+2K+2, the cows at positions a2a_2 and b2b_2 swap places.
  • And so on.

For each cow, find how many distinct positions in the line she will ever occupy.

Input Format

The first line contains NN and KK. The next KK lines each contain (a1,b1)(aK,bK)(a_1,b_1)\ldots(a_K,b_K) (1ai<biN1 \le a_i < b_i \le N).

Output Format

Output NN lines. The ii-th line contains the number of distinct positions that cow ii can reach.

5 4
1 3
1 2
2 3
2 4
4
4
3
4
1

Hint

  • Cow 11 can reach positions {1,2,3,4}\{1,2,3,4\}.
  • Cow 22 can reach positions {1,2,3,4}\{1,2,3,4\}.
  • Cow 33 can reach positions {1,2,3}\{1,2,3\}.
  • Cow 44 can reach positions {1,2,3,4}\{1,2,3,4\}.
  • Cow 55 never moves, so she never leaves position 55.

Test Point Properties:

  • Test points 1-5 satisfy N100,K200N \le 100, K \le 200.
  • Test points 6-10 satisfy N2000,K4000N \le 2000, K \le 4000.
  • Test points 11-20 have no additional constraints.

Problem provided by: Chris Zhang.

Translated by ChatGPT 5