#P7298. [USACO21JAN] Dance Mooves G

[USACO21JAN] Dance Mooves G

Problem Description

Farmer John's cows are showing off their newest dance moves.

At the beginning, all NN cows (2N1052 \le N \le 10^5) stand in a line, and cow ii is in position ii. A 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). During minute i=1Ki = 1 \ldots K of the dance, the cows in positions aia_i and bib_i swap positions. The same KK swaps happen again during minutes K+12KK+1 \ldots 2K, again during minutes 2K+13K2K+1 \ldots 3K, and so on, repeating periodically for a total of MM minutes (1M10181 \le M \le 10^{18}). In other words:

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

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

Note: For this problem, the time limit for each test case is twice the default time limit.

Input Format

The first line contains NN, KK, and MM. The next KK lines each contain (ai,bi)(a_i, b_i) (1ai<biN1 \le a_i < b_i \le N).

Output Format

Output NN lines. Line ii should contain the number of distinct positions that cow ii can reach.

6 4 7
1 2
2 3
3 4
4 5
5
4
3
3
3
1

Hint

After 77 minutes, the cows in each position are [3,4,5,2,1,6][3, 4, 5, 2, 1, 6].

  • Cow 11 can reach positions {1,2,3,4,5}\{1, 2, 3, 4, 5\}.
  • 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 {2,3,4}\{2, 3, 4\}.
  • Cow 55 can reach positions {3,4,5}\{3, 4, 5\}.
  • Cow 66 never moved, so she has never left position 66.

Testdata Properties:

  • Testdata 11-55 satisfy N100,K200N \le 100, K \le 200.
  • Testdata 66-1010 satisfy M=1018M = 10^{18}.
  • Testdata 1111-2020 have no additional constraints.

Problem credits: Chris Zhang.

Translated by ChatGPT 5