forked from encrypted-def/basic-algo-lecture
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path11724.cpp
42 lines (39 loc) · 808 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
// Authored by : BaaaaaaaaaaarkingDog
// Co-authored by : -
// http://boj.kr/888234c79cf844af82b67a3c9518b1dd
#include <bits/stdc++.h>
using namespace std;
vector<int> adj[1005];
bool vis[1005];
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
int n, m;
// 그래프 입력 처리
cin >> n >> m;
while(m--){
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
// BFS
int num = 0;
for(int i = 1; i <= n; i++){
if(vis[i]) continue;
num++;
queue<int> q;
q.push(i);
vis[i] = true;
while(!q.empty()){
int cur = q.front();
q.pop();
for(auto nxt : adj[cur]){
if(vis[nxt]) continue;
q.push(nxt);
vis[nxt] = true;
}
}
}
cout << num;
}