raghuvanshraj

841.cpp

Aug 4th, 2021
653
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  *    author:   vulkan
  3.  *    created:  03.08.2021 07:50:40 PM
  4. **/
  5. #include <bits/stdc++.h>
  6.  
  7. using namespace std;
  8.  
  9. void bfs(vector<vector<int>> &adj_list, int u, vector<bool> &visited) {
  10.     int n = adj_list.size();
  11.     queue<int> qu;
  12.     qu.push(u);
  13.     visited[u] = true;
  14.     while (not qu.empty()) {
  15.         int x = qu.front();
  16.         qu.pop();
  17.         for (int y : adj_list[x]) {
  18.             if (not visited[y]) {
  19.                 visited[y] = true;
  20.                 qu.push(y);
  21.             }
  22.         }
  23.     }
  24. }
  25.  
  26. bool canVisitAllRooms(vector<vector<int>> &rooms) {
  27.     int n = rooms.size();
  28.     vector<bool> visited(n);
  29.     bfs(rooms, 0, visited);
  30.  
  31.     return find(visited.begin(), visited.end(), false) == visited.end();
  32. }
  33.  
  34. int main(int argc, char const *argv[]) {
  35.     int n;
  36.     cin >> n;
  37.     vector<vector<int>> rooms(n);
  38.     for (int i = 0; i <= n - 1; ++i) {
  39.         int sz;
  40.         cin >> sz;
  41.         rooms[i] = vector<int>(sz);
  42.         for (int j = 0; j <= sz - 1; ++j) {
  43.             cin >> rooms[i][j];
  44.         }
  45.     }
  46.  
  47.     cout << canVisitAllRooms(rooms);
  48.  
  49.     return 0;
  50. }
RAW Paste Data