forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
41 lines (29 loc) · 760 Bytes
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/// Source : https://leetcode.com/problems/keys-and-rooms/description/
/// Author : liuyubobobo
/// Time : 2018-09-30
#include <iostream>
#include <vector>
using namespace std;
/// DFS
/// Time Compexity: O(V + E)
/// Space Complexity: O(V)
class Solution {
public:
bool canVisitAllRooms(vector<vector<int>>& rooms) {
int V = rooms.size();
vector<bool> visited(V, false);
return dfs(rooms, 0, visited) == V;
}
private:
int dfs(const vector<vector<int>>& rooms, int v, vector<bool>& visited){
visited[v] = true;
int res = 1;
for(int next: rooms[v])
if(!visited[next])
res += dfs(rooms, next, visited);
return res;
}
};
int main() {
return 0;
}