https://www.acmicpc.net/problem/2606
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
vector<int> a[101];
bool check[101];
void dfs(int start) {
check[start] = true;
for (int i = 0; i < a[start].size(); i++) {
if (!check[a[start][i]]) {
dfs(a[start][i]);
}
}
}
int main() {
int n, m;
cin >> n >> m;
for (int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
a[u].push_back(v);
a[v].push_back(u);
}
for (int i = 1; i <= n; i++) {
sort(a[i].begin(), a[i].end());
}
dfs(1);
int cnt = 0;
for (int i = 2; i <= n; i++) {
if (check[i]) {
cnt++;
}
}
cout << cnt;
return 0;
}
'algorithm > dfs,bfs' 카테고리의 다른 글
[프로그래머스] 단어 변환 (0) | 2020.08.25 |
---|---|
[백준/2667] 단지번호붙이기 (0) | 2020.04.06 |
[백준/1260] DFS와 BFS (0) | 2020.04.04 |
댓글