D_L3

SDA - 2020-2021 - test5 - task 1

Dec 11th, 2023
876
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.14 KB | None | 0 0
  1. #include <cmath>
  2. #include <cstdio>
  3. #include <vector>
  4. #include <iostream>
  5. #include <algorithm>
  6. #include <unordered_map>
  7. #include <queue>
  8. #include <unordered_set>
  9.  
  10.  
  11. using namespace std;
  12.  
  13. unordered_map<int, unordered_set<int>> graph;
  14. vector<bool> result;
  15.  
  16. bool isRoute(queue<int>& route){
  17.     int curr = route.front();
  18.     route.pop();
  19.     while(!route.empty()){
  20.         if(graph[curr].find(route.front()) == graph[curr].end())
  21.             return false;
  22.         curr = route.front();
  23.         route.pop();
  24.     }
  25.     return true;
  26. }
  27.  
  28. int main() {
  29.     int m, num, neighb;
  30.     cin >> m;
  31.     for(int i = 0; i < m; i++){
  32.         cin >> neighb;
  33.         for(int j= 0; j < neighb; j++){
  34.             cin >> num;
  35.             graph[i].insert(num);
  36.             graph[num].insert(i);
  37.         }
  38.     }
  39.     int q, c;
  40.     cin >> q;
  41.     for(int i = 0; i < q; i++){
  42.         queue<int> route;
  43.         cin >> c;
  44.         for(int j = 0; j < c; j++){
  45.             cin >> num;
  46.             route.push(num);
  47.         }
  48.         result.push_back(isRoute(route));
  49.     }
  50.    
  51.     for(int i : result){
  52.         cout << i << " ";
  53.     }
  54.     return 0;
  55. }
  56.  
Advertisement
Add Comment
Please, Sign In to add comment