-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConnectedComponents.cpp
More file actions
50 lines (35 loc) · 820 Bytes
/
ConnectedComponents.cpp
File metadata and controls
50 lines (35 loc) · 820 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <bits/stdc++.h>
using namespace std;
int n;
void mark_connections(vector<int>array[],int*check,int k){
check[k] = true;
for(int i : array[k]){
if(check[i])continue;
mark_connections(array,check,i);
}
}
int solve(vector<int>array[],int*check,int n){
int ans = 0;
for(int i = 0 ; i < n ; i ++){
if(check[i])continue;
else{ans++;}
mark_connections(array,check,i);
}
return ans;
}
int main(){
int m;
cin >> n >> m;
vector<int>array[n];
int check[n];
for(int i = 0 ; i < n ; i ++)check[i] = 0;
for(int i = 0 ; i < m ; i ++){
int x,y;
cin >> x >> y;
array[x-1].push_back(y-1);
array[y-1].push_back(x-1);
}
int a = solve(array,check,n);
cout << a;
return 0;
}