forked from AdarshAddee/Hacktoberfest2022_for_Beginers
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request AdarshAddee#170 from GarvitV957/patch-2
Created connected components cpp
- Loading branch information
Showing
1 changed file
with
77 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |