#P199. [Sleeping Cup #7] Back Translation

[Sleeping Cup #7] Back Translation

Person in Charge

Attention

The sample cases for this problem are not located in subtask 0. Instead, they are placed elsewhere:

Sample Data File Subtask ID Test Point
Sample 1 1.in / 1.out Subtask 1 #1-1
7.in / 7.out Subtask 2 #2-1
11.in / 11.out Subtask 3 #3-1
Sample 2 2.in / 2.out Subtask 1 #1-2
8.in / 8.out Subtask 2 #2-2
12.in / 12.out Subtask 3 #3-2

Each sample will be judged three times.

Subtask dependencies are as follows:

Subtask ID Depends on
Subtask 1 none
Subtask 2
Subtask 3
Subtask 4 Subtasks 1, 2, and 3

File I/O is required (translation.in / translation.out).

Problem Background

In 1922, the Commercial Press in Shanghai published the first Chinese translation of Don Quixote, titled The Magic Knight. Translator Lin Shu, who knew no Western languages, completed this Spanish classic with the help of his friend Chen Jialin, who read an English version aloud to him. Thus Lin Shu half-translated, half-recreated Don Quixote in classical Chinese, condensing the first 55 chapters into four sections.

A century later, Spanish sinologist Alicia Relinque translated this Chinese Don Quixote back into Spanish. The Ingenious Gentleman Don Quixote of La Mancha left 17th-century Spain, sojourned in China as The Magic Knight, and finally returned to Spain—completing a perfect adventure. Last month, the Cervantes Institute launched the Chinese–Spanish edition of The Magic Knight simultaneously in Madrid, Shanghai, and Beijing. The volume contains Lin Shu’s original text alongside Relinque’s Spanish re-translation, enriched with extensive notes and a detailed introduction.

This linguistic odyssey began eight years ago when the Cervantes Institute in Beijing exhibited Chinese translations of Cervantes’ works—collected over twenty years by Chinese bibliophile Liu Ruiming. Among the treasures was the 1934 edition of Lin Shu’s The Magic Knight—“a seemingly ordinary notebook, yet the brightest jewel in the exhibition,” as Imma González Bou, director of the Cervantes Institute in Shanghai, recalled. She initiated the re-translation project, determined to rescue this forgotten Chinese version. Under Relinque’s tireless efforts, the Spanish Magic Knight was born.

Imma sought to see how Lin Shu transformed the protagonist’s original image, thereby revealing how Don Quixote was received in early 20th-century China. Luis García Montero, director of the Cervantes Institute, noted that readers can sense the cultural parallels and contrasts between Spain and China through Don Quixote’s shifting portrayals.

From Spanish to English, English to Chinese, and Chinese back to Spanish, Lin Shu and Relinque’s cross-temporal collaboration allowed Don Quixote to complete a perfect closed loop.

(Source: https://www.jiemian.com/article/6014287.html)

A work can be translated into another language and then translated back? Sleeping Goose was intrigued and decided to start with a single word, exploring the secret of back-translation.

Problem Description

To gather first-hand data, Sleeping Goose interviewed translator Long Long Giraffe and learned the following:

  • For a certain meaning X, there are nn corresponding words in various languages, where word ii belongs to language cic_i.
  • To facilitate translation, Long Long Giraffe arranged semantically similar words consecutively, forming a directed cycle $\ldots \to 1 \to 2 \to \ldots \to n \to 1 \to \ldots$
  • Words of the same language that are too similar have been removed, so adjacent words in the cycle never share the same language.
  • The semantic deviation between two words aa and bb is defined as the minimum number of edges that must be traversed along the directed cycle from aa to bb (not necessarily equal to the reverse direction).
  • When translating word ii into language kk (language kk must differ from the language of word ii), Long Long Giraffe chooses the word jj that
    • belongs to language kk, and
    • minimizes the semantic deviation from ii to jj.

Now Sleeping Goose wants to know: for every word ii in the directed cycle, what is the minimum number of translations needed to bring the word back to ii itself?

Input Format

Multiple test cases.
First line: an integer TT, the number of test cases.

For each test case:

  • First line: an integer nn.
  • Second line: nn positive integers c1,c2,,cnc_1, c_2, \ldots, c_n.

Guarantees:

  • n5n \ge 5 and 1cii1 \le c_i \le i.
  • Words 11 and nn are not in the same language.
  • For i2i \ge 2, words ii and i1i-1 are not in the same language.
  • If language ii does not appear in the input, then language i+1i+1 does not appear either.
  • For i2i \ge 2, the first occurrence of language ii (if any) is strictly later than the first occurrence of language i1i-1.

Output Format

For each test case, output one line of n positive integers: the kk-th integer is the minimum number of translations required to bring word kk back to itself.

Samples

5
5
1 2 1 2 3
5
1 2 1 3 2
5
1 2 3 1 2
5
1 2 3 1 3
5
1 2 3 2 3
2 2 3 4 2
2 3 4 2 2
3 4 2 2 2
4 2 2 2 3
2 2 2 3 4
5
16
1 2 3 4 2 3 4 1 3 1 3 4 2 3 4 2
16
1 2 3 4 3 2 4 1 3 1 3 4 2 3 4 2
16
1 2 3 4 3 4 2 1 3 1 3 4 2 3 4 2
16
1 2 3 4 3 4 1 3 1 3 2 4 2 3 4 2
16
1 2 3 4 3 4 1 3 1 2 3 4 2 3 4 2
4 4 4 4 5 4 5 4 5 6 6 4 4 5 4 5
4 4 4 4 5 5 5 4 5 6 6 4 4 5 4 5
4 4 4 4 5 6 4 4 5 6 6 4 4 5 4 5
3 4 3 3 4 5 4 5 6 6 3 4 5 4 4 4
3 4 3 3 4 5 4 5 6 3 4 4 5 5 5 5

Sample 1 Explanation

For the third test case, the optimal back-translation strategies for the five words are:

  1. $\text{Word 1}\xrightarrow{\text{Language 3}}\text{Word 3}\xrightarrow{\text{Language 2}}\text{Word 5}\xrightarrow{\text{Language 1}}\text{Word 1}$
  2. $\text{Word 2}\xrightarrow{\text{Language 1}}\text{Word 4}\xrightarrow{\text{Language 2}}\text{Word 5}\xrightarrow{\text{Language 1}}\text{Word 1}\xrightarrow{\text{Language 2}}\text{Word 2}$
  3. $\text{Word 3}\xrightarrow{\text{Language 1}}\text{Word 4}\xrightarrow{\text{Language 3}}\text{Word 3}$
  4. $\text{Word 4}\xrightarrow{\text{Language 3}}\text{Word 3}\xrightarrow{\text{Language 1}}\text{Word 4}$
  5. $\text{Word 5}\xrightarrow{\text{Language 3}}\text{Word 3}\xrightarrow{\text{Language 2}}\text{Word 5}$

Data Range

For 100%100\% of the data: 1T101 \le T \le 10, 5n1055 \le n \le 10^5.

There are four equally-weighted subtasks:

  1. n100n \le 100.
  2. Each language appears in at most 55 words.
  3. There are at most 55 distinct languages.
  4. No additional constraints.