My issue
probel
My code
#include <bits/stdc++.h>
using namespace std;
int main() {
// your code goes here
}
Learning course: Graphs
Problem Link: Connected Components - using DFS Practice Problem in Graphs - CodeChef
probel
#include <bits/stdc++.h>
using namespace std;
int main() {
// your code goes here
}
Learning course: Graphs
Problem Link: Connected Components - using DFS Practice Problem in Graphs - CodeChef
@paveldoichev
please refer the following code
#include <bits/stdc++.h>
using namespace std;
vector<int> adjList[200004];
bool visited[200004];
void dfs(int node){
visited[node] = true;
for(int v: adjList[node]){
if(!visited[v]){
dfs(v);
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin >> n >> m;
for(int i = 1; i <= n; i++){
visited[i] = false;
}
for(int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
adjList[u].push_back(v);
adjList[v].push_back(u);
}
int components = 0;
for(int i = 1; i <= n; i++){
if(!visited[i]){
components++;
dfs(i);
}
}
cout << components;
return 0;
}