-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path11724.cpp
More file actions
48 lines (43 loc) · 817 Bytes
/
11724.cpp
File metadata and controls
48 lines (43 loc) · 817 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
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#define io ios::sync_with_stdio(false); cin.tie(0);
using namespace std;
vector<int> graph[499501];
bool visited[1001];
queue<int> q;
void bfs(int R){
visited[R]=true;
q.push(R);
while(!q.empty()){
int u=q.front();
q.pop();
for(int v:graph[u]){
if(visited[v]==false){
visited[v]=true;
q.push(v);
}
}
}
}
int main()
{
io;
int N,M,res=0;
cin>>N>>M;
for(int i=0;i<M;i++){
int u,v;
cin>>u>>v;
graph[u].push_back(v);
graph[v].push_back(u);
}
for(int i=1;i<=N;i++){
if(visited[i]==false){
bfs(i);
res++;
}
}
cout<<res;
return 0;
}