#P4218. [CTSC2010] 珠宝商

    ID: 3149 远端评测题 8000ms 512MiB 尝试: 0 已通过: 0 难度: 9 上传者: 标签>字符串2010WC/CTSC/集训队枚举,暴力分治后缀树

[CTSC2010] 珠宝商

Description

Louis.PS is a shrewd jeweler whose necklaces have a unique construction, largely because his production method is different. Each time Louis.PS arrives in a country, he chooses a path to traverse the cities of that country. Upon arriving at a city, he makes a bead using the material popular in that city, and strings the beads together in the order the cities are visited to make a necklace. To prevent competition between cities from affecting sales, the path never visits the same city twice (because if the material from city AA is used more than that from city BB in the necklace, promotion in city BB may be affected). After years of consumer research, Louis.PS has learned how to judge how attractive a necklace is to consumers. Specifically, he has identified the features of popular necklaces and, based on this, has created a long necklace (which he calls the feature necklace). For a necklace for sale, the more times it appears in the feature necklace, the more popular it is.

To simplify the complex reality, we make the following assumptions. In each country, some cities are directly connected by roads; for any two distinct cities, there is exactly one path connecting them (i.e., the country is connected and has no cycle). For each city, we use a lowercase letter to denote the material type popular in that city. Thus, we can represent a necklace by a string containing only lowercase letters. We call the string corresponding to the feature necklace the feature string, denoted as EigenString[1M]\mathit{EigenString}[1\ldots M], where MM is the length of the feature necklace. For a necklace, suppose its corresponding string is P[1L]P[1\ldots L], where LL is the length of this necklace. If there exists a positive integer KK such that EigenString[KK+L1]=P[1L]\mathit{EigenString}[K\ldots K+L-1]=P[1\ldots L], we say this necklace appears once in the feature necklace. The number of such positive integers KK is the occurrence count of this necklace in the feature necklace, denoted as Popularity(P)\mathit{Popularity}(P).

Louis.PS uses the mathematical concept of expectation to evaluate whether a country is suitable for material collection. For a country with NN cities, let Stru,v\mathit{Str}_{u,v} denote the string corresponding to the necklace obtained along the path starting at uu and ending at vv (in general, Stru,v\mathit{Str}_{u,v} and Strv,u\mathit{Str}_{v,u} are different). Then

$$\mathit{Expectation}=\frac{\sum_{u,v}{\mathit{Popularity}(\mathit{Str}_{u,v})}}{N^2}$$

For the following example (solid lines in the figure indicate that the endpoints are directly connected by a road):

N=3N=3, and the popular material types are a,a,b\tt{a,a,b}.

$$\mathit{Expectation}=\frac{3+1+2+1+3+1+1+1+2}{9}=\frac53$$

For a given country, Louis.PS wants to know the value of Expectation\mathit{Expectation}, but the computation workload is large. As the apprentice, you certainly would not miss the chance to impress your boss.

Input Format

The first line contains two integers N,MN, M, the number of cities and the length of the feature necklace.

The next N1N-1 lines each contain two integers x,yx, y, indicating that city xx and city yy are directly connected by a road. Cities are numbered from 1 to NN.

The next line contains a string of length NN consisting only of lowercase letters. The ii-th character indicates the material type popular in city ii.

The last line contains a string of length MM consisting only of lowercase letters, denoting the feature string.

Output Format

Output a single integer, which is N2×ExpectationN^2 \times \mathit{Expectation}.

3 5
1 2
1 3
aab
abaab
15

Hint

  • 20% of the testdata satisfy M1000M \leq 1000.
  • 40% of the testdata satisfy N8000,M50000N \leq 8000, M \leq 50000.
  • For 100% of the testdata, N,M50000N, M \leq 50000.

Translated by ChatGPT 5