Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <queue>
- #include <set>
- #include <fstream>
- using namespace std;
- const int maxn=5555;
- const int INF = 2e9;
- vector<pair<int, int> > graph[maxn];
- int bfs(int S, int k) {
- vector<bool> visited(maxn, false);
- visited[S] = true;
- queue<int> 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;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement