-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHamiltonion_Path.cpp
More file actions
40 lines (40 loc) · 1.03 KB
/
Hamiltonion_Path.cpp
File metadata and controls
40 lines (40 loc) · 1.03 KB
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
//https://practice.geeksforgeeks.org/problems/hamiltonian-path2522/1
/*
A Hamiltonian path, is a path in an undirected graph that visits each vertex exactly once.
*/
Class Solution
{
bool dfs(int node,vector<int>&vis,vector<int>adj[],int V,int cnt){
vis[node] =1;
cnt++;
if(cnt == V) return true;
for(auto it:adj[node]){
if(!vis[it]){
if(dfs(it,vis,adj,V,cnt) == true) return true;
}
}
cnt--;
vis[node]=0;
return false;
}
public:
bool check(int N,int M,vector<vector<int>> Edges)
{
// code here
vector<int>adj[N];
for(auto it:Edges){
int u = it[0]-1; int v = it[1]-1;
adj[u].push_back(v);
adj[v].push_back(u);
}
vector<int>vis(N,0);
int cnt = 0;
for(int i = 0;i<N;i++){
if(!vis[i]){
cnt= 0 ;
if(dfs(i,vis,adj,N,cnt) == true) return true;
}
}
return false;
}
};