Codeforces Round 285 (Div. 1)
E
Misha and LCP on Tree
Codeforces
3000
原题
← D→
翻译中…

Misha has a tree with characters written on the vertices. He can choose two vertices $*s*$ and $*t*$ of this tree and write down characters of vertices lying on a path from $*s*$ to $*t*$. We'll say that such string corresponds to pair $(*s*, *t*)$.

翻译中…

Misha has $*m*$ queries of type: you are given $4$ vertices $*a*$, $*b*$, $*c*$, $*d*$; you need to find the largest common prefix of the strings that correspond to pairs $(*a*, *b*)$ and $(*c*, *d*)$. Your task is to help him.

翻译中…

Input

翻译中…

The first line contains integer $*n*$ ($1 ≤ *n* ≤ 300 000$) — the number of vertices in the tree.

翻译中…

Next follows a line consisting of $*n*$ small English letters. The $*i*$-th character of the string corresponds to the character written on the $*i*$-th vertex.

翻译中…

Next $*n* - 1$ lines contain information about edges. An edge is defined by a pair of integers $*u*$, $*v*$ ($1 ≤ *u*, *v* ≤ *n*$, $*u* ≠ *v*$), separated by spaces.

翻译中…

The next line contains integer $*m*$ ($1 ≤ *m* ≤ 1 000 000$) — the number of queries.

翻译中…

Next $*m*$ lines contain information about queries. A query is defined by four integers $*a*$, $*b*$, $*c*$, $*d*$ ($1 ≤ *a*, *b*, *c*, *d* ≤ *n*$), separated by spaces.

翻译中…

Output

翻译中…

For each query print the length of the largest common prefix on a separate line.

翻译中…

样例

Input #1
6
bbbabb
2 1
3 2
4 3
5 2
6 5
6
2 5 3 1
1 5 2 3
5 6 5 6
6 3 4 1
6 2 3 4
2 2 4 5
Output #1
2
2
2
0
1
0
InputCopy OutputCopy
题目信息
题号
504E
时间限制
8 s
内存限制
512 MB
标签
二分
DFS 及类似
哈希
后缀结构
树

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。