#P4218. [CTSC2010] 珠宝商
[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 is used more than that from city in the necklace, promotion in city 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 , where is the length of the feature necklace. For a necklace, suppose its corresponding string is , where is the length of this necklace. If there exists a positive integer such that , we say this necklace appears once in the feature necklace. The number of such positive integers is the occurrence count of this necklace in the feature necklace, denoted as .
Louis.PS uses the mathematical concept of expectation to evaluate whether a country is suitable for material collection. For a country with cities, let denote the string corresponding to the necklace obtained along the path starting at and ending at (in general, and 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):
, and the popular material types are .

For a given country, Louis.PS wants to know the value of , 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 , the number of cities and the length of the feature necklace.
The next lines each contain two integers , indicating that city and city are directly connected by a road. Cities are numbered from 1 to .
The next line contains a string of length consisting only of lowercase letters. The -th character indicates the material type popular in city .
The last line contains a string of length consisting only of lowercase letters, denoting the feature string.
Output Format
Output a single integer, which is .
3 5
1 2
1 3
aab
abaab
15
Hint
- 20% of the testdata satisfy .
- 40% of the testdata satisfy .
- For 100% of the testdata, .
Translated by ChatGPT 5
京公网安备 11011102002149号