Advertisement
Oppenheimer

connected components

Aug 19th, 2022
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.38 KB | None | 0 0
  1. unordered_map<int,vector<int>> adj;
  2. vector<int> vis;
  3.  
  4. void dfs(int root){
  5. vis[root] = 1;
  6.     for(int v : adj[root]){
  7.         if(vis[v] == 0)dfs(v);
  8.     }
  9.  
  10. }
  11.  
  12. int main(){
  13.     int cnt =0;
  14.     for(int node = 0;node < n; node++){
  15.         if(vis[node] == 0){
  16.         dfs(node);
  17.             cnt++;
  18.         }
  19.     }
  20.    
  21.     cout << "Connected components : " << cnt ;
  22.     return 0;
  23. }
  24.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement