-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBaekJoon11724.cpp
More file actions
50 lines (44 loc) · 804 Bytes
/
BaekJoon11724.cpp
File metadata and controls
50 lines (44 loc) · 804 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
//BaekJoon 11724 연결 요소의 개수
#include <iostream>
#include <string>
#include <vector>
#include <stack>
using namespace std;
int n, m;
vector<int> arr[1001];
bool visited[1001] = { false, };
int dfs() {
stack<int> st;
int cnt = 0;
for (int i = 1; i <= n; i++) {
if (visited[i]) continue;
st.push(i);
cnt++;
visited[i] = true;
while (!st.empty()) {
int cur = st.top();
st.pop();
for (int j = 0; j < arr[cur].size(); j++) {
int nxt = arr[cur][j];
if (visited[nxt]) continue;
st.push(nxt);
visited[nxt] = true;
}
}
}
return cnt;
}
int main(void)
{
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m;
for (int i = 1; i <= m; i++) {
int a, b;
cin >> a >> b;
arr[a].push_back(b);
arr[b].push_back(a);
}
cout << dfs();
return 0;
}