#include <iostream>
#include <queue>
using namespace std;
int map[1001][1001];
int visited[1001];
int cnt = 0;
int N, M;
void dfs(int x) {
visited[x] = 1;
for (int i = 1; i <=N; i++) {
if (!visited[i] && map[x][i]) {
visited[i] = 1;
dfs(i);
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> N>>M;
for (int i = 0; i < M; i++) {
int a, b;
cin >> a >> b;
map[a][b] = map[b][a] = 1; //์ฐ๊ฒฐ์์ ์
๋ ฅ
}
for (int i = 1; i <= N; i++) {
if (!visited[i]) {
dfs(i);
cnt++;
}
}
cout << cnt;
}
'Algorithm ๐ง๐ปโ๐ป > ๋ฐฑ์ค(BOJ)' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๋ฐฑ์ค,c++] 11779๋ฒ - ์ต์๋น์ฉ ๊ตฌํ๊ธฐ2 (0) | 2021.10.31 |
---|---|
[๋ฐฑ์ค,c++] 11728๋ฒ - ๋ฐฐ์ดํฉ์น๊ธฐ (0) | 2021.10.31 |
[๋ฐฑ์ค,c++] 11725๋ฒ - ํธ๋ฆฌ์ ๋ถ๋ชจ ์ฐพ๊ธฐ (0) | 2021.10.31 |
[๋ฐฑ์ค,c++] 11722๋ฒ - ๊ฐ์ฅ ๊ธด ๊ฐ์ํ๋ ๋ถ๋ถ ์์ด (0) | 2021.10.31 |
[๋ฐฑ์ค,c++] 11721๋ฒ - ์ด ๊ฐ์ฉ ๋์ด ์ถ๋ ฅํ๊ธฐ (0) | 2021.10.31 |
[๋ฐฑ์ค,c++] 11720๋ฒ - ์ซ์์ ํฉ (0) | 2021.10.31 |
๋๊ธ