-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path11724.cpp
More file actions
43 lines (39 loc) · 733 Bytes
/
Copy path11724.cpp
File metadata and controls
43 lines (39 loc) · 733 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
#include <iostream>
#include <queue>
using namespace std;
int N,M;
queue<int> q;
int graph[1001][1001];
int visited[1001];
int cnt=0;
void BFS(int s){
q.push(s);
int flag=0;
visited[s]=1;
while(!q.empty()){
int v=q.front();
q.pop();
for(int i=1; i<=N; i++){
if (graph[v][i]==1 && visited[i]==0){
q.push(i);
visited[i]=1;
flag=1;
}
}
}
}
int main(){
cin >> N >>M;
for(int i=0; i<M; i++){
int u,v; cin >> u >>v;
graph[u][v]=1;
graph[v][u]=1;
}
for(int i=1;i<=N;i++){
if(visited[i]==0){
BFS(i);
cnt++;
}
}
cout << cnt;
}