Descriptions:
DSM(Data Structure Master) once learned about tree when he was preparing for NOIP(National Olympiad in Informatics in Provinces) in Senior High School. So when in Data Structure Class in College, he is always absent-minded about what the teacher says.
The experienced and knowledgeable teacher had known about him even before the first class. However, she didn’t wish an informatics genius would destroy himself with idleness. After she knew that he was so interested in ACM(ACM International Collegiate Programming Contest), she finally made a plan to teach him to work hard in class, for knowledge is infinite.
This day, the teacher teaches about trees." A tree with nn nodes, can be defined as a graph with only one connected component and no cycle. So it has exactly edges…" DSM is nearly asleep until he is questioned by teacher. " I have known you are called Data Structure Master in Graph Theory, so here is a problem. " " A tree with nn nodes, which is numbered from to nn. Edge between each two adjacent vertexes and has a value , you’re asked to answer the number of edge whose value is no more than during the path between and ."" If you can’t solve the problem during the break, we will call you DaShaMao(Foolish Idiot) later on."
The problem seems quite easy for DSM. However, it can hardly be solved in a break. It’s such a disgrace if DSM can’t solve the problem. So during the break, he telephones you just for help. Can you save him for his dignity?
Input
In the first line there are two integers , represent the number of vertexes on the tree and queries
The next lines, each line contains three integers ,, indicates there is an undirected edge between nodes and with value .
The next mm lines, each line contains three integers , be consistent with the problem given by the teacher above.
Output
For each query, just print a single line contains the number of edges which meet the condition.
样例输入1
3 3
1 3 2
2 3 7
1 3 0
1 2 4
1 2 7
样例输出1
0
1
2
样例输入2
5 2
1 2 1000000000
1 3 1000000000
2 4 1000000000
3 5 1000000000
2 3 1000000000
4 5 1000000000
样例输出2
2
4
题意就是个点,条边,然后每条边都有自己的权值,个询问,每个询问就是从一个点到另外一个点的路径上有多少满足权值不大于的边有多少条。
这个就是HDU4417的进阶版,加了一个LCA函数。
AC代码: