11724 연결 요쇼의 개수
2019. 9. 6. 08:16ㆍ알고리즘/백준
모든 정점을 방문하면서 BFS를 통해 연결 요소의 개수를 구하는 문제
문제: https://www.acmicpc.net/problem/11724
깃허브주소: https://github.com/surinoel/boj/blob/master/11724.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <vector> | |
#include <iostream> | |
using namespace std; | |
bool visit[1001]; | |
vector<int> a[1001]; | |
void go(int idx) { | |
if (visit[idx]) { | |
return; | |
} | |
visit[idx] = true; | |
for (int i = 0; i < a[idx].size(); i++) { | |
go(a[idx][i]); | |
} | |
return; | |
} | |
int main(void) { | |
ios_base::sync_with_stdio(false); | |
cin.tie(nullptr); | |
int n, m; | |
cin >> n >> m; | |
for (int i = 0; i < m; i++) { | |
int x, y; | |
cin >> x >> y; | |
a[x].push_back(y); | |
a[y].push_back(x); | |
} | |
int ans = 0; | |
for (int i = 1; i <= n; i++) { | |
if (!visit[i]) { | |
ans += 1; | |
go(i); | |
} | |
} | |
cout << ans << '\n'; | |
return 0; | |
} |
'알고리즘 > 백준' 카테고리의 다른 글
1707 이분 그래프 (0) | 2019.09.06 |
---|---|
9328 열쇠 (0) | 2019.09.06 |
4991 로봇 청소기 (0) | 2019.09.05 |
1248 맞춰봐 (0) | 2019.09.03 |
2529 부등호 (0) | 2019.09.03 |