category-wise-problems

contains category wise problems(data structures, competitive) of popular platforms.

View the Project on GitHub mayankdutta/category-wise-problems

841. Keys and Rooms

DFS
DFS Implementation ```cpp class Solution { public: bool canVisitAllRooms(vector<vector>& rooms) { int n = rooms.size(); int m = rooms[0].size(); vector used(n + 1, false); auto dfs = [&](const auto& self, const int& u) -> void { used[u] = true; for (const auto& v: rooms[u]) { if (!used[v]) { self(self, v); } } }; for (const auto& i: rooms[0]) dfs(dfs, i); for (int i = 1; i < n; i++) { if (!used[i]) { return false; } } return true; } }; ``` </details> ##### BFS
BFS Implementation ```cpp class Solution { public: bool canVisitAllRooms(vector<vector>& rooms) { queue qu; vector used(rooms.size(), false); for (auto& i: rooms[0]) { used[i] = true; qu.push(i); } while (!qu.empty()) { auto u = qu.front(); qu.pop(); for (const auto& v: rooms[u]) { if (used[v]) continue; used[v] = true; qu.push(v); } } for (int i = 1; i < rooms.size(); i++) { if (!used[i]) { return false; } } return true; } }; ``` </details>