Codeforces Round 406 (Div. 1)
D
Rap God
Codeforces
3400
原题
← CE →
翻译中…

Rick is in love with Unity. But Mr. Meeseeks also love Unity, so Rick and Mr. Meeseeks are "love rivals".

翻译中…

Unity loves rap, so it decided that they have to compete in a rap game (battle) in order to choose the best. Rick is too nerds, so instead he's gonna make his verse with running his original algorithm on lyrics "Rap God" song.

翻译中…

image

翻译中…

His algorithm is a little bit complicated. He's made a tree with $*n*$ vertices numbered from $1$ to $*n*$ and there's a lowercase english letter written on each edge. He denotes $*str*(*a*, *b*)$ to be the string made by writing characters on edges on the shortest path from $*a*$ to $*b*$ one by one (a string of length equal to distance of $*a*$ to $*b*$). Note that $*str*(*a*, *b*)$ is reverse of $*str*(*b*, *a*)$ and $*str*(*a*, *a*)$ is empty.

翻译中…

In order to make the best verse he can, he needs to answer some queries, but he's not a computer scientist and is not able to answer those queries, so he asked you to help him. Each query is characterized by two vertices $*x*$ and $*y*$ ($*x* ≠ *y*$). Answer to this query is the number of vertices like $*z*$ such that $*z* ≠ *x*, *z* ≠ *y*$ and $*str*(*x*, *y*)$ is lexicographically larger than $*str*(*x*, *z*)$.

翻译中…

String $*x*  =  *x*_{1}*x*_{2}...*x*_{|*x*|}$ is lexicographically larger than string $*y*  =  *y*_{1}*y*_{2}...*y*_{|*y*|}$, if either $|*x*|  >  |*y*|$ and $*x*_{1}  =  *y*_{1},  *x*_{2}  =  *y*_{2},  ...,  *x*_{|*y*|}  =  *y*_{|*y*|}$, or exists such number $*r* (*r*  <  |*x*|,  *r*  <  |*y*|)$, that $*x*_{1}  =  *y*_{1},  *x*_{2}  =  *y*_{2},  ...,  *x*_{*r*}  =  *y*_{*r*}$ and $*x*_{*r*  +  1}  >  *y*_{*r*  +  1}$. Characters are compared like their ASCII codes (or alphabetic order).

翻译中…

Help Rick get the girl (or whatever gender Unity has).

翻译中…

Input

翻译中…

The first line of input contain two integers $*n*$ and $*q*$ ($2 ≤ *n* ≤ 20000$, $1 ≤ *q* ≤ 20000$) — number of vertices in tree and number of queries respectively.

翻译中…

The next $*n* - 1$ lines contain the edges. Each line contains two integers $*v*$ and $*u*$ (endpoints of the edge) followed by an English lowercase letter $*c*$ ($1 ≤ *v*, *u* ≤ *n*, *v* ≠ *u*$).

翻译中…

The next $*q*$ line contain the queries. Each line contains two integers $*x*$ and $*y*$ ($1 ≤ *x*, *y* ≤ *n*, *x* ≠ *y*$).

翻译中…

Output

翻译中…

Print the answer for each query in one line.

翻译中…

Note

翻译中…

Here's the tree of first sample testcase:

翻译中…

image

翻译中…

Here's the tree of second sample testcase:

翻译中…

image

翻译中…

In this test:

翻译中…
  • $*str*(8, 1)$ = poo
  • $*str*(8, 2)$ = poe
  • $*str*(8, 3)$ = po
  • $*str*(8, 4)$ = pop
  • $*str*(8, 5)$ = popd
  • $*str*(8, 6)$ = popp
  • $*str*(8, 7)$ = p
翻译中…

So, for the first query, image and for the third query image is the answer.

翻译中…

样例

Input #1
4 3
4 1 t
3 2 p
1 2 s
3 2
1 3
2 1
Output #1
0
1
1
InputCopy OutputCopy
Input #2
8 4
4 6 p
3 7 o
7 8 p
4 5 d
1 3 o
4 3 p
3 2 e
8 6
3 7
8 1
4 3
Output #2
6
1
3
1
InputCopy OutputCopy
题目信息
题号
786D
时间限制
7 s
内存限制
256 MB
标签
数据结构
DFS 及类似
哈希
字符串
树

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