#P8406. [COCI 2021/2022 #6] Palindromi

[COCI 2021/2022 #6] Palindromi

Problem Description

You are given a 01\texttt{01} sequence of length nn, indexed by 1,2,,n1,2,\dots,n. Initially, each character represents a string of length 11. In one concatenation operation, you need to choose two strings aa and bb, delete them, and replace them with the string abab, i.e., write all characters of aa first and then write all characters of bb.

These nn initial strings need to be merged into one string using n1n-1 concatenation operations. The pair of strings chosen in the ii-th operation is described by an index pair (ai,bi)(a_i,b_i), meaning that the two strings to be concatenated are the one containing the character with index aia_i and the one containing the character with index bib_i. It is guaranteed that the characters with indices aia_i and bib_i are not in the same string.

The palindrome value of some string ww is defined as the number of distinct palindromic substrings in ww. We define a palindrome as a string that reads the same from left to right and from right to left. A substring of a string is defined as a string that can be obtained by deleting one or more characters from the beginning and/or the end of the string.

For each concatenation operation, output the palindrome value of the resulting string.

Input Format

The first line contains an integer nn, representing the number of characters.

The second line contains a 01\texttt{01} string of length nn, representing the initial strings.

The next n1n-1 lines each contain two integers aia_i, bib_i, representing the ii-th concatenation operation.

Output Format

Output n1n-1 lines, where the ii-th line is the palindrome value of the string obtained after the ii-th concatenation operation.

3
010
1 2
2 3
2
3
5
00111
4 1
1 5
2 1
3 1
2
3
4
5

8
10010000
7 5
4 2
3 6
1 3
6 8
5 3
1 2
2
2
2
3
4
6
8

Hint

Sample Explanation 3:

After each concatenation, the newly created strings are: 00,10,00,100,1000,001000\tt 00, 10,00, 100, 1000,001000 and 00100010\tt 00100010.

Their palindrome values are given in the sample output. For example, the palindrome value of 00100010\tt 00100010 is 88, because the string contains 88 palindromic substrings: 0,00,000,10001,0100010,1010\tt 0, 00, 000, 10001,0100010, 1010 and 0010000100.

Constraints:

For 9%9\% of the testdata: 1n1001\le n\le100.

For 18%18\% of the testdata: 1n10001\le n\le1000.

For 27%27\% of the testdata: ai=1,bi=i+1a_i = 1, b_i = i + 1.

For 100%100\% of the testdata: 1n1051\le n\le10^5, 1ai,bin1\le a_i, b_i\le n.

The scoring for this problem is the same as COCI 2021-2022#6, with a full score of 110110 points.

Translated by ChatGPT 5