(Analysis by Nick Wu)

To reword the problem more precisely, we have an undirected weighted tree. Define $f(v, w)$ to be the minimum weight over all edges on the path from $v$ to $w$. We want to answer several queries for a given vertex $v$ and $k$ of the form - how many vertices $w$ exist where $f(v, w) \ge k$?

To answer this query for a given vertex, we can start by doing a BFS from $v$. We never want to traverse an edge with edge weight strictly less than $k$, so we ignore those edges. We can then count how many other vertices we have visited.

import java.io.*;
import java.util.*;
public class mootube {
public static void main(String[] args) throws IOException {
PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("mootube.out")));
int n = Integer.parseInt(st.nextToken());
int q = Integer.parseInt(st.nextToken());
for(int i = 0; i < n; i++) {
}
for(int a = 1; a < n; a++) {
int x = Integer.parseInt(st.nextToken())-1;
int y = Integer.parseInt(st.nextToken())-1;
int w = Integer.parseInt(st.nextToken());
}
for(int query = 0; query < q; query++) {
int threshold = Integer.parseInt(st.nextToken());
int start = Integer.parseInt(st.nextToken())-1;
int ret = 0;
boolean[] seen = new boolean[n];
seen[start] = true;
while(!queue.isEmpty()) {
int curr = queue.removeFirst();
for(Edge out: edges[curr]) {
if(!seen[out.d] && out.w >= threshold) {
seen[out.d] = true;
ret++;
}
}
}
pw.println(ret);
}
pw.close();
}

static class Edge {
public int d, w;
public Edge(int a, int b) {
d=a;
w=b;
}
}

}