-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolutions.cpp
28 lines (27 loc) · 866 Bytes
/
solutions.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
class Solution {
public:
// Function to detect cycle in an undirected graph.
bool isCycle(vector<vector<int>>& adj) {
int n = adj.size();
int m = adj[0].size();
vector<bool> visited(n,false);
for(int i=0; i<n; i++){
if(!visited[i]){
queue<vector<int>> q;
q.push({i, -1});
while(!q.empty()){
vector<int> nodes = q.front();
q.pop();
int parent = nodes[1];
int val = nodes[0];
visited[val] = true;
for(auto i:adj[val]){
if(visited[i] and i!=parent) return true;
if(!visited[i]) q.push({i, val});
}
}
}
}
return false;
}
};