CCCPaste

Untitled

#include 
#include 
#include 
#include 
#include 
using namespace std;
const int maxn=5555;
const int INF = 2e9;
vector > graph[maxn];
int bfs(int S, int k) {
    vector visited(maxn, false);
    visited[S] = true;
    queue q;
    q.push(S);
    q.push(INF);
    int res = 0;
    while(!q.empty()) {
        int node = q.front();
        q.pop();
        int dist = q.front();
        q.pop();
        
        if(node != S and dist >= k) {
            res++;
        }
        for(int i = 0; i < (int) graph[node].size(); i++) {
            int neighbour = graph[node][i].first;
            int weight = graph[node][i].second;
            
            if(!visited[neighbour]) {
                visited[neighbour] = true;
                q.push(neighbour);
                q.push(min(dist, weight));
            }
        }
    }
    return res;
}
int main() {
    ifstream cin("mootube.in");
    ofstream cout("mootube.out");
    int n, q;
    cin >> n >> q;
    
    for(int i = 0; i < n - 1; i++) {
        int a, b;
        cin >> a >> b;
        a--; b--;
        int k;
        cin >> k;
        
        graph[a].push_back(make_pair(b, k));
        graph[b].push_back(make_pair(a, k));
    }
    
    for(int i = 0; i < q; i++) {
        int a, k;
        cin >> k >> a;
        a--;
        
        cout << bfs(a, k) << endl;
        
    }
    
    return 0;
}