8615: Palindromic Tree

内存限制:256 MB 时间限制:10 S
题面:传统 评测方式:Special Judge 上传者:
提交:0 通过:0

题目描述

You are given a tree with nnn nodes. A character 0\texttt{0}0 or 1\texttt{1}1 is written on each node. You are also given qqq queries. For each query, you need to calculate the number of different palindromic substrings of the string obtained from the simple path from node uuu to node vvv.

We say two strings sss and ttt are different if their lengths are different or there exists at least one position iii satisfying si≠tis_i \neq t_isi=ti. A palindromic string is a string that reads the same forward or backward.

输入格式

The first line of the input contains an integer TTT (1≤T≤1041 \leq T \leq 10^41T104), indicating the number of test cases.
For each test case, the first line contains two integers n,qn, qn,q (1≤n,q≤1051 \leq n, q \leq 10^51n,q105), indicating the number of nodes and the number of queries.
The second line contains a string of length nnn consisting of 0\texttt{0}0 or 1\texttt{1}1, indicating the characters written on nodes from 111 to nnn.
Each of the following n−1n - 1n1 lines contains two integers u,vu, vu,v (1≤u,v≤n1 \leq u, v \leq n1u,vn), indicating an edge connecting nodes uuu and vvv. It is guaranteed that these edges form a tree.
Each of the following qqq lines contains two integers u,vu, vu,v (1≤u,v≤n1 \leq u, v \leq n1u,vn), indicating a query with nodes uuu and vvv.
It is guaranteed that ∑n≤105\sum n \leq 10^5n105 and ∑q≤105\sum q \leq 10^5∑q105 over all test cases.

输出格式

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

输入样例 复制

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

分类标签