-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path11724.cpp
60 lines (49 loc) · 794 Bytes
/
11724.cpp
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
51
52
53
54
55
56
57
58
59
60
#include <iostream>
#include <queue>
using namespace std;
int arr[1000 + 1][1000 + 1];
bool check[1000 + 1];
int N, M, ans;
queue<int> q;
void dfs(int num){
check[num] = true;
for(int i = 1; i <= N; i++){
if(arr[num][i] && !check[i])
dfs(i);
}
return;
}
void bfs(int num){
q.push(num);
check[num] = true;
while (!q.empty()){
int now = q.front();
q.pop();
for(int i = 1; i <= N; i++){
if(arr[now][i] && !check[i]){
check[i] = true;
q.push(i);
}
}
}
return;
}
int main(){
ios::sync_with_stdio(false); cin.tie(NULL);
cin >> N >> M;
int u, v = 0;
for(int i = 1; i <= M; i++){
cin >> u >> v;
arr[u][v] = 1;
arr[v][u] = 1;
}
for(int i = 1; i <= N; i++){
if(!check[i]){
// dfs(i);
bfs(i);
ans++;
}
}
cout << ans;
return 0;
}