Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- #define ll long long
- bool visited[10000]={0};
- int counter=0;
- vector <int> adj[10000];
- void dfs(int k){
- visited[k]=true;
- for (auto u:adj[k]){
- if (!visited[u]){
- dfs(u);
- }
- }
- }
- void connected(int x){
- if (!visited[x]){
- counter++;
- dfs(x);
- }
- }
- int main()
- {
- int n,m;
- cin>>n>>m;
- for (int i=0; i<m; i++){
- int x,y;
- cin>>x>>y;
- adj[x].push_back(y);
- adj[y].push_back(x);
- }
- for (int i=1; i<=n; i++){
- connected(i);
- }
- cout<<counter<<"\n";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement