Query on tree - MarisaOJ: Marisa Online Judge

Query on tree

Time limit: 1000 ms
Memory limit: 256 MB
You are given a weighted tree with $n$ vertices. In this tree: - $d(u, v)$ represents the weight of the minimum edge on the simple path from vertex $u$ to vertex $v$. Your task is to handle $q$ queries of the form $(u, k)$, count the number of vertices $v$ for which $d(u, v) = k$. ### Input - The first line contains two integers $n,q$. - The next $n - 1$ lines, each line contains three integers $u, v, w$, there is an edge of weight $w$ between $u$ and $v$. - The next $q$ lines, each line contains two integers $u, k$, a query. ### Output - Print an integer is the answer for each query. ### Constraints - $1 \le n,q \le 10^5$. - $1 \le u, v \le n$. - $1 \le w, k \le n$. ### Example Input: ``` 4 2 1 2 1 2 3 2 2 4 3 3 2 2 1 ``` Output: ``` 2 1 ```