Palindromic Tree
题号:NC239616
时间限制:C/C++/Rust/Pascal 10秒,其他语言20秒
空间限制:C/C++/Rust/Pascal 256 M,其他语言512 M
Special Judge, 64bit IO Format: %lld

题目描述

See Problem N for PDF statements.
You are given a tree with n nodes. A character or is written on each node. You are also given q queries. For each query, you need to calculate the number of different palindromic substrings of the string obtained from the simple path from node u to node v.

We say two strings s and t are different if their lengths are different or there exists at least one position i satisfying . A palindromic string is a string that reads the same forward or backward.

输入描述:

The first line of the input contains an integer T (), indicating the number of test cases.

For each test case, the first line contains two integers n, q (), indicating the number of nodes and the number of queries.

The second line contains a string of length n consisting of or , indicating the characters written on nodes from 1 to n.

Each of the following n - 1 lines contains two integers u, v (), indicating an edge connecting nodes u and v. It is guaranteed that these edges form a tree.

Each of the following q lines contains two integers u, v (), indicating a query with nodes u and v.

It is guaranteed that and over all test cases.

输出描述:

For each query, output the answer in a single line.
示例1

输入

复制
1
5 5
01110
1 2
1 3
2 4
2 5
1 1
1 2
3 4
3 5
4 5

输出

复制
1
2
4
4
3