Advertisement
tepyotin2

Untitled

Oct 5th, 2023
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.42 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. vector<vector<pair<int, int>>> neighbors;
  4. vector<bool> visited;
  5. int threshold;
  6. int num_reachable;
  7. /** searches all vertices that can be reached through the current video v */
  8. void search_videos(int v) {
  9.     visited[v] = true;
  10.     for (const pair<int, int>& n: neighbors[v]) {
  11.         /*
  12.          * only visit nonvisited videos whose relevance
  13.          * is greater than the current threshold
  14.          */
  15.         if (!visited[n.first] && n.second >= threshold) {
  16.             num_reachable++;
  17.             search_videos(n.first);
  18.         }
  19.     }
  20. }
  21. int main() {
  22.     freopen("mootube.in", "r", stdin);
  23.     int video_num;
  24.     int query_num;
  25.     cin >> video_num >> query_num;
  26.     neighbors = vector<vector<pair<int, int>>>(video_num);
  27.     for (int e = 0; e < video_num - 1; e++) {
  28.         int a, b;
  29.         int relevance;
  30.         cin >> a >> b >> relevance;
  31.         a--;
  32.         b--;
  33.         neighbors[a].push_back({b, relevance});
  34.         neighbors[b].push_back({a, relevance});
  35.     }
  36.     freopen("mootube.out", "w", stdout);
  37.     for (int q = 0; q < query_num; q++) {
  38.         int start;
  39.         cin >> threshold >> start;
  40.         start--;
  41.        
  42.         // reset our global variables for the current query
  43.         num_reachable = 0;
  44.         visited = vector<bool>(video_num);
  45.         search_videos(start);
  46.        
  47.         cout << num_reachable << '\n';
  48.     }
  49. }
  50.  
  51.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement