Skip to content

Commit 2af2646

Browse files
committed
Added solution - LeetHub
1 parent 384002f commit 2af2646

File tree

1 file changed

+96
-0
lines changed

1 file changed

+96
-0
lines changed
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
//{ Driver Code Starts
2+
#include<bits/stdc++.h>
3+
using namespace std;
4+
5+
// } Driver Code Ends
6+
class Solution
7+
{
8+
public:
9+
10+
11+
bool dfs(int node,int currVisited,vector<int>&visited,vector<int>adj [],int N){
12+
13+
visited[node]=1;
14+
15+
if(N==currVisited)
16+
{
17+
return true;
18+
}
19+
for(auto it:adj[node]){
20+
if(!visited[it])
21+
{
22+
bool ans=dfs(it,currVisited+1,visited,adj,N);
23+
if(ans==true)
24+
{
25+
return true;
26+
}
27+
else{
28+
continue;
29+
}
30+
}
31+
}
32+
visited[node]=0;
33+
return false;
34+
}
35+
bool check(int N,int M,vector<vector<int>> Edges)
36+
{
37+
// code here
38+
39+
// start to end visit karna hai
40+
41+
// dfs
42+
43+
vector<int>adj[N+1];
44+
45+
vector<int>visited(N+1,0);
46+
47+
// making a adjacency list
48+
49+
for(auto e:Edges)
50+
{
51+
int u=e[0];
52+
int v=e[1];
53+
adj[u].push_back(v);
54+
adj[v].push_back(u);
55+
}
56+
for(int i=1;i<=N;i++)
57+
{
58+
if(!visited[i]){
59+
bool ans=dfs(i,1,visited,adj,N);
60+
if(ans==true)
61+
{
62+
return true;
63+
}
64+
else{
65+
continue;
66+
}
67+
}
68+
}
69+
return false;
70+
}
71+
};
72+
73+
74+
//{ Driver Code Starts.
75+
int main()
76+
{
77+
int t;
78+
cin>>t;
79+
while(t--){
80+
int N,M,X,Y;
81+
cin>>N>>M;
82+
vector<vector<int>> Edges;
83+
for(int i=0;i<M;i++)
84+
{
85+
cin>>X>>Y;
86+
Edges.push_back({X,Y});
87+
}
88+
Solution obj;
89+
if(obj.check(N,M,Edges)){
90+
cout<<"1"<<endl;
91+
}
92+
else
93+
cout<<"0"<<endl;
94+
}
95+
}
96+
// } Driver Code Ends

0 commit comments

Comments
 (0)