Skip to content

Commit

Permalink
Created connected components cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
GarvitV957 committed Oct 2, 2022
1 parent a48b99d commit 69ef062
Showing 1 changed file with 77 additions and 0 deletions.
77 changes: 77 additions & 0 deletions C++/Connected_components.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
░░░░░░░░░▄░░░░░░░░░░░░░░▄░░░░
░░░░░░░░▌▒█░░░░░░░░░░░▄▀▒▌░░░
░░░░░░░░▌▒▒█░░░░░░░░▄▀▒▒▒▐░░░
░░░░░░░▐▄▀▒▒▀▀▀▀▄▄▄▀▒▒▒▒▒▐░░░
░░░░░▄▄▀▒░▒▒▒▒▒▒▒▒▒█▒▒▄█▒▐░░░
░░░▄▀▒▒▒░░░▒▒▒░░░▒▒▒▀██▀▒▌░░░
░░▐▒▒▒▄▄▒▒▒▒░░░▒▒▒▒▒▒▒▀▄▒▒▌░░
░░▌░░▌█▀▒▒▒▒▒▄▀█▄▒▒▒▒▒▒▒█▒▐░░
░▐░░░▒▒▒▒▒▒▒▒▌██▀▒▒░░░▒▒▒▀▄▌░
░▌░▒▄██▄▒▒▒▒▒▒▒▒▒░░░░░░▒▒▒▒▌░
▀▒▀▐▄█▄█▌▄░▀▒▒░░░░░░░░░░▒▒▒▐░
▐▒▒▐▀▐▀▒░▄▄▒▄▒▒▒▒▒▒░▒░▒░▒▒▒▒▌
▐▒▒▒▀▀▄▄▒▒▒▄▒▒▒▒▒▒▒▒░▒░▒░▒▒▐░
░▌▒▒▒▒▒▒▀▀▀▒▒▒▒▒▒░▒░▒░▒░▒▒▒▌░
░▐▒▒▒▒▒▒▒▒▒▒▒▒▒▒░▒░▒░▒▒▄▒▒▐░░
░░▀▄▒▒▒▒▒▒▒▒▒▒▒░▒░▒░▒▄▒▒▒▒▌░░
░░░░▀▄▒▒▒▒▒▒▒▒▒▒▄▄▄▀▒▒▒▒▄▀░░░
░░░░░░▀▄▄▄▄▄▄▀▀▀▒▒▒▒▒▄▄▀░░░░░
░░░░░░░░░▒▒▒▒▒▒▒▒▒▒▀▀░░░░░░░░
*/

// Github: GarvitV957

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define lli long long int
#define vi vector<int>
#define vvi vector<vi>
#define vll vector<long long>
#define vb vector<bool>
#define pb push_back
#define pii pair<int,int>
#define all(x) x.begin(),x.end()

int N=1e6 +1;
vvi adj(N);
vi vis(N,0);

vvi cc;
vi current_comp;

void dfs(int i){
vis[i]=1;
current_comp.pb(i);
for(auto v:adj[i]){
if(!vis[v]){
dfs(v);
}
}
}

int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);

int n,e;
cin>>n>>e;
for(int i=0;i<e;i++){
int x,y;
cin>>x>>y;
adj[x].pb(y),adj[y].pb(x);
}
int c=0;
for(int i=1;i<=n;i++){
if(!vis[i]){
current_comp.clear();
dfs(i);
cc.pb(current_comp);
c++;
}
}
cout<<c<<endl;
cout<<cc.size()<<endl;
return 0;
}

0 comments on commit 69ef062

Please sign in to comment.