CF786D.Rap God

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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.

His algorithm is a little bit complicated. He's made a tree with nn vertices numbered from 11 to nn and there's a lowercase english letter written on each edge. He denotes str(a,b)str(a,b) to be the string made by writing characters on edges on the shortest path from aa to bb one by one (a string of length equal to distance of aa to bb ). Note that str(a,b)str(a,b) is reverse of str(b,a)str(b,a) and str(a,a)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 xx and yy ( xyx≠y ). Answer to this query is the number of vertices like zz such that zx,zyz≠x,z≠y and str(x,y)str(x,y) is lexicographically larger than str(x,z)str(x,z) .

String x=x1x2...xxx=x_{1}x_{2}...x_{|x|} is lexicographically larger than string y=y1y2...yyy=y_{1}y_{2}...y_{|y|} , if either |x|>|y| and x1=y1,x2=y2,...,xy=yyx_{1}=y_{1},x_{2}=y_{2},...,x_{|y|}=y_{|y|} , or exists such number r\ (r<|x|,r<|y|) , that x1=y1,x2=y2,...,xr=yrx_{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).

输入格式

The first line of input contain two integers nn and qq ( 2<=n<=200002<=n<=20000 , 1<=q<=200001<=q<=20000 ) — number of vertices in tree and number of queries respectively.

The next n1n-1 lines contain the edges. Each line contains two integers vv and uu (endpoints of the edge) followed by an English lowercase letter cc ( 1<=v,u<=n,vu1<=v,u<=n,v≠u ).

The next qq line contain the queries. Each line contains two integers xx and yy ( 1<=x,y<=n,xy1<=x,y<=n,x≠y ).

输出格式

Print the answer for each query in one line.

输入输出样例

  • 输入#1

    4 3
    4 1 t
    3 2 p
    1 2 s
    3 2
    1 3
    2 1
    

    输出#1

    0
    1
    1
    
  • 输入#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
    

    输出#2

    6
    1
    3
    1
    

说明/提示

Here's the tree of first sample testcase:

Here's the tree of second sample testcase:

In this test:

  • str(8,1)str(8,1) = poo
  • str(8,2)str(8,2) = poe
  • str(8,3)str(8,3) = po
  • str(8,4)str(8,4) = pop
  • str(8,5)str(8,5) = popd
  • str(8,6)str(8,6) = popp
  • str(8,7)str(8,7) = p

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

首页