You are given an undirected tree of n vertices rooted at 1. Vertices are numbered from 1 to n. The ith node has the value ai. Then there are q queries, each of them contains two integers: x,k. For each query, you are required to count how many pairs of (i,j) that satisfy the following condition: 1. ai= x. 2. |ai aj| ≤ k. 3. The jth node is an ancestor of the ith node.
You are given an undirected tree (a connected acyclic undirected graph) of n vertices rooted at 1. Vertices are numbered from 1 to n. The ith node has the value ai. Then there are q queries, each of them contains two integers: x,k. For each query, you are required to count how many pairs of (i,j) that satisfy the following condition: 1. ai= x. 2. |ai− aj| ≤ k. 3. The jth node is an ancestor of the ith node.
(图片来源网络,侵删)